amit
amit

Reputation: 10261

where to include script files

i have divided various components of the page in different php file. In the navigation php file i have the objects i want to use in the javascript. where should i put the javascript <script ...> so that it loads fine? right now i am putting it in a completely seperate file header.php? but i dont think the javascript is picking objects from nav.php i hope i am making sense ;)

Upvotes: 3

Views: 168

Answers (5)

symcbean
symcbean

Reputation: 48357

Noet that the performance benefit you gain from repositioning the tags (or using more esoteric methods for avoiding blocking) is very small compared to the benefit of getting them cached correctly at the browser.

C.

Upvotes: 0

Matt
Matt

Reputation: 7212

My understanding is that the best speed comes from putting the script at the end of the page?

http://developer.yahoo.com/performance/rules.html

Put Scripts at the Bottom The problem caused by scripts is that they block parallel downloads. The HTTP/1.1 specification suggests that browsers download no more than two components in parallel per hostname. If you serve your images from multiple hostnames, you can get more than two downloads to occur in parallel. While a script is downloading, however, the browser won't start any other downloads, even on different hostnames.

In some situations it's not easy to move scripts to the bottom. If, for example, the script uses document.write to insert part of the page's content, it can't be moved lower in the page. There might also be scoping issues. In many cases, there are ways to workaround these situations.

An alternative suggestion that often comes up is to use deferred scripts. The DEFER attribute indicates that the script does not contain document.write, and is a clue to browsers that they can continue rendering. Unfortunately, Firefox doesn't support the DEFER attribute. In Internet Explorer, the script may be deferred, but not as much as desired. If a script can be deferred, it can also be moved to the bottom of the page. That will make your web pages load faster.

Upvotes: 0

Anthony Mills
Anthony Mills

Reputation: 8784

Generally the advice is to put the <script> at the bottom of your HTML page.

http://developer.yahoo.com/performance/rules.html

Upvotes: 0

DA.
DA.

Reputation: 40671

The standard suggestion is that you should put all of your SCRIPT links prior to your closing BODY tag at the bottom of your document. This streamlines network connections:

http://developer.yahoo.com/performance/rules.html

Upvotes: 1

Jerod Venema
Jerod Venema

Reputation: 44632

It doesn't matter where in the PHP rendering process you put it, it only matters that when the output HTML and javascript are combined, the HTML elements exist before you try to access them in javascript.

It's for this reason that most javascript toolkits have a function for executing javascript once the page elements are loaded, such as jquery's document.ready function.

Upvotes: 1

Related Questions