开发者

How does JavaScript behave in this situation?

开发者 https://www.devze.com 2022-12-09 17:55 出处:网络
For speed purposes, I\'m trying to decide whether to place my JavaS开发者_运维问答cript in the <head> or just before the </body> of my site. I prefer the head and am not concerned with the

For speed purposes, I'm trying to decide whether to place my JavaS开发者_运维问答cript in the <head> or just before the </body> of my site. I prefer the head and am not concerned with the extra HTTP request, just the behavior of this script.

Here are the two code snippets, which are loaded back-to-back (Google Analytics):

Script 1:

// Loaded externally:
// <script type="text/javascript" src="google_analytics_1.js"></script>

var gaJsHost = (
    ( "https:" == document.location.protocol ) ?
    "https://ssl." :
    "http://www."
);

document.write( unescape(
    "%3Cscript src='" +
    gaJsHost +
    "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"
) );

Script 2:

// Loaded externally:
// <script type="text/javascript" src="google_analytics_2.js"></script>

try {
    var pageTracker = _gat._getTracker( "UA-0000000-0" );
    pageTracker._trackPageview();
}
catch( err ) { }

With the code above, does JavaScript delay sending the rest of the page until it retrieves ga.js, or does it instead say, "okay, I've got the request for ga.js queued up, now I will continue to send the rest of the page while we wait for it to download to the client..."?

My instinct tells me it is linear, and that the page will be delayed until ga.js has been completely downloaded by the client. Is that right?


The parsing of the page will pause while loading the ga.js file, but the server will continue sending the rest of the page. When the script has been loaded and executed, it will continue with the rest of the page.

So, the script and the rest of the page will load in parallel, but it won't touch any of the rest of the page until the script has been processed.


Nop. It won't be delayed. It will queue up the first request and continue with the second one, actually it's the browser that handle's it.

0

精彩评论

暂无评论...
验证码 换一张
取 消