user658589
user658589

Reputation:

How can I load js files in specific pages using jQueryMobile?

It's a known fact that jQueryMobile loads pages with ajax and is not including in DOM the header content in every pages.

I need to load a custom js file in some pages, how can I achieve this? Until now I have placed the .js files in the body, but there are some problems with the code there too so it's not a good workaround. Until I can find a solution I will use the rel="external" workaround, but I really need to find an answer to my question.

Upvotes: 2

Views: 3454

Answers (2)

user700284
user700284

Reputation: 13620

You could try including the custom script within the data-role="page" div in pages where you want to use those javascript.

From JQM docs:

Another approach for page-specific scripting would be to include scripts at the end of the body element. If you include your custom scripting this way, be aware that these scripts will execute when that page is loaded via Ajax or regular HTTP, so if these scripts are the same on every page, you'll likely run into problems. If you're including scripts this way, we'd recommend enclosing your page content in a data-role="page" element, and placing scripts that are referenced on every page outside of that element. Scripts that are unique to that page can be placed in that element, to ensure that they execute when the page is fetched via Ajax.

Upvotes: 2

Michael Allen
Michael Allen

Reputation: 5828

You could use some javascript to dynamically add the js file to the DOM.

This is demoed here: http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml

Upvotes: 0

Related Questions