Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should scripts be located in a jQuery Mobile page for best performance?

Most people recommend scripts tags be placed at the bottom of the page for performance reasons - jQuery: Move JavaScript to the bottom of the page?

How does this practice apply to jQuery Mobile web pages?

Examples I have seen place jQuery and jQuery Mobile scripts in the head tag. Should other scripts be placed in the head tag as well? What is best practice?

like image 394
James Newton-King Avatar asked Nov 13 '10 07:11

James Newton-King


People also ask

Where should you place the jQuery Mobile js file?

jquery-mobile Getting started with jquery-mobile Setup Create a library (or lib) folder inside of your application (inside the www folder). Create a jquery-mobile folder inside the lib folder (I've named mine jqm). Place all of the jquery-mobile files that you have extracted inside of the jquery-mobile folder.

What is jQuery Mobile page?

The page is the primary unit of interaction in jQuery Mobile and is used to group content into logical views that can be animated in and out of view with page transitions.

What is role in jQuery Mobile?

A page in jQuery Mobile consists of an element with a data-role="page" attribute. Within the "page" container, any valid HTML markup can be used, but for typical pages in jQuery Mobile, the immediate children of a "page" are divs with data-role="header" , class="ui-content" , and data-role="footer" .

Does jQuery run on mobile?

Every tablet and smartphone in the world today is supported by jQuery Mobile, including browsers such as iOS, Android, Chrome, Firefox, BlackBerry and Symbian and native platforms such as Apache Cordova (PhoneGap).


3 Answers

Here, i think the same answer counts as for desktop pages. If the javascript is vital for the page functionality you put it at the top. If it's complementary, you place it at the bottom.

like image 186
dubbelj Avatar answered Nov 08 '22 00:11

dubbelj


While this might not be related to performance as speed, it is important in the jquery mobile context that you properly add scripts to pages. Plain old $(function(){}) will surprise you. It doesn't work in some cases.

When a link is clicked jquery mobile fetches the page with ajax and adds its content to the currently open page ignoring everything you put in the <head>. [This is true as of jqm 1.0a2]

There are two ways of adding scripts that will work:

  1. putting all scripts in a single .js file and link them in the head of all pages of your app and work with events that jqm provides (pageshow and pagecreate ones).
  2. putting scripts at the bottom of the body and NOT require DOMready to fire.

The second way might be better for performance in big applications, where there are a lot of scripts that the user might not use (not visiting certain pages). I would recommend the first way - it's cleaner and jqm seems to encourage it.

A hybrid of both might be what's best - adding a pageshow event handler thet would trigger some default function from each loaded page.

[edit]

see Limitations here at the bottom of the page.

like image 42
naugtur Avatar answered Nov 08 '22 00:11

naugtur


Best practice is on the bottom. usually jquery script are executed when DOM is loaded so being down on the page has no effect on display, but it does have an effect on speed, as it will allow other elements to load previously.

like image 30
Elzo Valugi Avatar answered Nov 08 '22 00:11

Elzo Valugi