What are the best practices for dividing pages in jQuery Mobile?

Exploring the capabilities of jQuery Mobile has led me to delve into setting up an application and its multiple pages. My hope is for the framework to seamlessly load pages with transitions while ensuring each page remains distinct from one another. However, it appears that when a page is loaded via Ajax, remnants of JavaScript and CSS from the previous page may still linger:

Illustrative Example 1 - JavaScript

Consider this scenario in page1.html:

<script>
    setInterval(function() {
        console.info('Interval' + (new Date()));
    }, 1000);
</script>

If I proceed to load page2.html, the interval continues running uninterrupted. Upon returning to page1, a new interval initiates alongside the existing one, leading to multiple concurrent intervals as I navigate back and forth.

Illustrative Example 2 - CSS

An additional concern arises regarding styling. Imagine two developers independently working on separate pages, both incorporating an element named my-element. In page1, this element is styled as follows:

<style>
    #my-element {
        color: red;
    }
</style>

<span id="my-element">This one is red</span>

Conversely, in page2, the same element lacks any specific styling:

<span id="my-element">This one has no style</span>

Upon navigating from page1.html and then to page2.html, my-element unexpectedly adopts the red coloration, despite not being styled in page2.html.

Ergo, I find myself pondering two fundamental questions:

  1. It increasingly appears that the current method employed by jQuery Mobile in loading pages lacks scalability. Is there a feasible way to prompt jQuery Mobile to load pages devoid of any lingering artifacts, ensuring subsequent pages remain unaffected by remnants of JS or CSS?

  2. If not, how can one effectively manage multiple pages within jQuery Mobile in a sustainable manner?

Answer №1

The concept being discussed is the essence of JQM's functionality. When you load a page using ajax, it remains in the DOM along with the original page until you trigger a non-ajax page load using 'data-ajax=false' or 'rel=external'.

With ajax loading, the DOM usually holds onto at least two pages - the initial page and any additional pages loaded via ajax (which are removed once the next page is loaded).

This method enables smooth transitions and shared functions across different pages.

You can still easily target specific pages with js/css. In my applications, I always have a controller.js file that manages page-specific actions by binding to various JQM events (such as pagebeforeshow, pageshow...). Similarly for page-specific css, just use the page id attribute for targeted styling.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Material-UI: Error - getMuiTheme function is not defined

After recently updating my material-ui to version 0.15.4, I encountered an issue while trying to make it work. The error message states that getMuiTheme is not a function, despite the fact that the relevant JavaScript file is present in the folder location ...

What is the best way to receive a notification when a user chooses any option in a Material UI Autocomplete React component?

My dilemma involves using an autocomplete feature that doesn't behave as expected. I am looking to detect when an option is selected, regardless of whether it's the same as the last selection. The onChange event I'm using only triggers if th ...

Using C# for making HTTP requests and pairing it with JavaScript

I have encountered an issue while using C# HttpWebRequest to retrieve data from a webpage. The problem arises when some of the data is updated through javascript/ajax after the page has already loaded, causing me to not receive it in the response string. I ...

Utilize the Algolia search-insights library in conjunction with React

Trying to integrate the Search-Insights Library from Algolia with React using npm for installation instead of just adding it to the header as shown in the example. Example of implementation in React Click here for React implementation example <script ...

Navigating HTML Symbol Entities: Mastering the Art of Floating

I need to figure out how to make an HTML symbol entity stay fixed on the browser screen. It's for a Sign-In page and I'm trying to have a down arrow float next to a sign in link, positioned approximately 25px from the left and 10 px from the top ...

The method request.getParameter in Servlet may sometimes result in a null

My website utilizes JQuery to make an Ajax call to a servlet. function sendAjax() { $.ajax({ url: "/AddOrUpdateServlet", type: 'POST', dataType: 'json', ...

Ways to retrieve the "this" keyword in a <script setup> Vue Single File Component

Currently, I am developing a basic scaffold for vite, vue, and vuetify utilizing typescript. My goal is to implement the script setup version of SFC Vue. <script setup lang="ts"> One particular challenge I am facing is how to access proper ...

Retrieve array elements randomly using a specified number

I'm trying to figure out how to randomly display multiple items from an array. I've successfully displayed one random item, but now I want to be able to show a variable number of random items based on user input. For example, if the user inputs 2 ...

What are the steps to create a dynamic background color effect for an element?

I'm having trouble cycling through different colors or background images. The code I tried only runs once and doesn't loop as expected. .landing-background { background-color: red; -webkit-animation-name: example; /* Chrome, Safari, Oper ...

Identify unique MongoDb instances within an array of data

My list contains various unique items - search = alluk.distinct('Object of search') I am interested in counting each item individually. Presently, I am counting them manually in the following way - alluk.find({'Object of search':&ap ...

Adjust the appearance of an element based on user selection from a dropdown menu under certain circumstances

I am working on creating a dropdown menu using a JavaScript array. My goal is to display a specific span element when certain options are selected. For instance, I want the span to be shown only when options "a" and "c" are selected, but not when option " ...

Is there a way to modify both an icon and text's color simultaneously when hovering with the mouse?

Recently, I created 3 icons with text underneath. You can view the code here. An issue arises when hovering over an icon - only the icon turns red while the text remains grey. However, if you hover over the text first, both the icon and text turn red. My ...

Challenges with handling JSON data in JavaScript

I am currently attempting to retrieve and parse JSON data in order to display it on a blank HTML file. Unfortunately, I keep encountering an issue where if I retrieve and parse the data, I receive an error stating Uncaught TypeError: Cannot read property & ...

What is the reason behind every single request being treated as an ajax request?

Recently, I embarked on a new application development journey using rails 4.0. However, I've encountered an unexpected situation where every request is being processed as an ajax request. For instance, consider this link: =link_to "View detail", pr ...

Utilizing regular expressions to search through a .md file in JavaScript/TS and returning null

I am currently using fs in JavaScript to read through a changelog.MD file. Here is the code snippet: const readFile = async (fileName: string) => { return promisify(fs.readFile)(filePath, 'utf8'); } Now I am reading my .md file with this fu ...

Place a gap at a specific spot within the boundary line

Here is a CSS code snippet that displays a horizontal line: .horizontalLineBottom { border-bottom:solid #6E6A6B; border-width:1px; } Is it possible to add space at a specific position on this line? For example, _________________________________ ...

What is the preferred method for updating a variable value - Ajax or PHP?

I'm in the process of creating a dropdown survey for new visitors using cookies to collect data, but I'm a bit confused on how to implement it. If a button is clicked, the onClick event will trigger var answer1 = answer1 + 1 , or something simil ...

How to address critical vulnerabilities found in a Vue.js project that relies on the 'vue-svg-loader' dependency, specifically impacting 'nth-check', 'css-select', and 'svgo'?

Attempting to launch a Vue version 2 project, encountered the following error: # npm audit report nth-check <2.0.1 Severity: high Inefficient Regular Expression Complexity in nth-check - https://github.com/advisories/GHSA-rp65-9cf3-cjxr fix available ...

JavaScript framework that is easily customizable to include support for XmlHttpRequest.onprogress, even if it needs to be emulated

Which JavaScript library or framework offers support for the "onprogress" event for XmlHttpRequest, even if it needs to be emulated using a plugin or extension? Alternatively, which JavaScript framework is the most straightforward to extend in order to add ...

Leveraging the Axios content delivery network for Vue Template presentation

I am looking to extract JSON data using the Axios library. By utilizing my {{ id }}, I aim to target a specific parameter in the request. Furthermore, I have an external script file for this purpose. const Home = { template: ` <div class="us ...