Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put Scripts at the Bottom

Tags:

javascript

Yslow recommends that you should put scripts at the bottom of the page.

Where exactly should I put them, after the body end tag?

My whole site runs in jquery, so all the javascript files are needed at the beginning. If I move them to the end, it might stop my site from working. That being the case, where instead should i put these scripts for best performance?

like image 239
Hacker Avatar asked Feb 09 '11 17:02

Hacker


People also ask

Why scripts are placed at bottom of the page?

When you place your JavaScript at the bottom of your HTML body, it gives the HTML time to load before any of the JavaScript loads, which can prevent errors, and speed up website response time.

Where can scripts be placed?

Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.

Does it matter if script is in head or body?

When you place a script in the head section, you will ensure that the script is loaded before anyone uses it. Scripts in the body section: Scripts to be executed when the page loads go in the body section. When you place a script in the body section it generates the content of the page.

Why JavaScript should be at the bottom?

If you include external js files at the bottom of your page, you give the priority of your HTTP requests to the visual display that will be presented to the client instead of to the logic of interaction or dynamics.


1 Answers

You should put them right before the closing body tag. Make sure your scripts are being initiated properly using

$(document).ready()

And you should have no issues.

like image 192
Loktar Avatar answered Sep 26 '22 20:09

Loktar