Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Move JavaScript to the bottom of the page?

We are developing large ASP.NET applications with lot of dynmically created pages containing ASCX controls. We use a lot of jQuery everywhere.

I have been reading that it would make sense to move the inline JavaScript code to the bottom of the page as it could delay the loading of the page when it's included "too early".

My question is now: Does this still make sense when working with jQuery?

Most of the code is executed in the ready handler, so I would expect that is does not slow down the loading of the page. In my case the multiple Usercontrols ASCX have all their own jQuery bits and pieces, and it would not be easy to move that all down in the rendered page.

like image 254
PeterFromCologne Avatar asked Nov 24 '08 19:11

PeterFromCologne


People also ask

How do I move JavaScript to the bottom of the page?

Lets assume that your plugin or theme is adding raw JavaScript in the header or between the content. Find the raw JavaScript code in the plugin or theme files, copy the JavaScript and save it in a . js file. Then use wp_register_script() function as shown above, to move JavaScript to the bottom.

How do you scroll to the end of a page in JavaScript?

The window. scrollTo method to scroll to the bottom of the page. window. scrollTo(0, document. body.

Should JavaScript be at the bottom?

Once JavaScript files have the purpose to add functionality - something happen after a button is clicked for example — there is no good reason to load the JS file before the button itself. So go ahead and place JS files at the bottom of the HTML, just before the closing body tag.

How do I move something in jQuery?

All you have to do is select the element(s) you want to move, then call an “adding” method such as append() , appendTo() or prepend() to add the selected elements to another parent element. jQuery automatically realises that the element(s) to add already exist in the page, and it moves the element(s) to the new parent.


2 Answers

Placing scripts late in the HTML is recommended because loading and executing scripts happens sequentially (one script at a time) and completely blocks the loading and parsing of images and CSS files meanwhile.

Large/lagged/slow-running scripts near the top of the page can cause unnecessary delay to the loading and rendering of the page content/layout.

Script's size (download time) and complexity (execution time (dom traversal, etc.)) factor in - but more importantly, the number of individual <script> HTTP requests matters far more (the fewer requests the better).

Using the "document.ready" handler lessens the delay caused by slow execution - but still leaves the problem of the sequential HTTP overhead.

Recommended reading: High Performance Web Sites by Nate Koeckley.

like image 156
Már Örlygsson Avatar answered Oct 26 '22 01:10

Már Örlygsson


You could model the different ways of ordering the JavaScript in Cuzillion to see how it affects page loading.

See the examples and this blog post for examples of how ordering of page elements can affect speed.

like image 45
Sam Hasler Avatar answered Oct 26 '22 03:10

Sam Hasler