Include to favorites
Log in Logout Register
Start Login Contact Help Photos What's new
Avanced Search
FAQ
RESULTS IN: TEXT IMAGES

Hello, Guest
Login  Register
Online: 109 visitors

Blogging (2)
Business (1)
Educational (2)
Gadgets (1)
Games (2)
High Tech News (1)
Internet (0)
PC (1)
PDA (0)
Photography (0)
Science (1)
Software (1)
Wireless (1)


Browse by date

<< Dicember 2008 >>
MonTueWedThrFriSatSun
1234567
891011121314
15161718192021
22232425262728
293031




Recent searches

Popular searches

Hot this month

Weblogs Archive


GADGETS AND GAMES DIRECTORY :: > Software Register Weblog >  Software Tech Weblogs - WEEKLYBITS.COM GADGETS AND GAMES DIRECTORY
Yahoo User Interface Blog
generated by http://wordpress.org/?v=2.3.3  en Blogger
SEND A FRIEND
Suscribing to  please login first
User: Login

Is a collection of industrial-grade JavaScript utilities and widgets that enable you to efficiently get the most out of today’s powerful web applications.
News and Articles about Designing and Developing wiht Yahoo! Libraries.Visit Yahoo! User Interface Blog
Address URLhttp://yuiblog.com/blog    Registered: 26-Apr-2008
Ads:

Send to email
Visit Non-blocking JavaScript Downloads Non-blocking JavaScript Downloads in DevelopmentPerformance
By Stoyan Stefanov
el 22-Jul-2008

Stoyan Stefanov.About the Author: Stoyan Stefanov is a Yahoo! web developer working for the Exceptional Performance team and leading the development of the YSlow performance tool. He also an open-source contributor, conference speaker and technical writer: his latest book is called Object-Oriented JavaScript.

External JavaScript files block downloads and hurt your page performance, but there is an easy way to work around this problem: use dynamic scripts tags and load scripts in parallel, improving the page loading speed and the user experience.

The problem: scripts block downloads

Let’s first take a look at what the problem is with the script downloads. The thing is that before fully downloading and parsing a script, the browser can’t tell what’s in it. It may contain document.write() calls which modify the DOM tree or it may even contain location.href and send the user to a whole new page. If that happens, any components downloaded from the previous page may never be needed. In order to avoid potentially useless downloads, browsers first download, parse and execute each script before moving on with the queue of other components waiting to be downloaded. As a result, any script on your page blocks the download process and that has a negative impact on your page loading speed.

Here’s how the timeline looks like when downloading a slow JavaScript file (exaggerated to take 1 second). The script download (the third row in the image) blocks the two-by-two parallel downloads of the images that follow after the script:

Timeline - Blocking behavior of JavaScript files

Here’s the example to test yourself.

Problem 2: number of downloads per hostname

Another thing to note in the timeline above is how the images following the script are downloaded two-by-two. This is because of the restriction of how many components can be downloaded in parallel. In IE <= 7 and Firefox 2, it’s two components at a time (following the HTTP 1.1 specs), but both IE8 and FF3 increase the default to 6.

You can work around this limitation by using multiple domains to host your components, because the restriction is two components per hostname. For more information of this topic check the article “Maximizing Parallel Downloads in the Carpool Lane” by Tenni Theurer.

The important thing to note is that JavaScripts block downloads across all hostnames. In fact, in the example timeline above, the script is hosted on a different domain than the images, but it still blocks them.

Scripts at the bottom to improve user experience

As Yahoo!’s Performance rules advise, you should put the scripts at the bottom of the page, towards the closing </body> tag. This doesn’t really make the page load faster (the script still has to load), but helps with the progressive rendering of the page. The user perception is that the page is faster when they can see a visual feedback that there is progress.

Non-blocking scripts

It turns out that there is an easy solution to the download blocking problem: include scripts dynamically via DOM methods. How do you do that? Simply create a new <script> element and append it to the <head>:

var js = document.createElement('script');
js.src = 'myscript.js';
var head = document.getElementsByTagName('head')[0];
head.appendChild(js);

Here’s the same test from above, modified to use the script node technique. Note that the third row in the image takes just as long to download, but the other resources on the page are loading simultaneously:

Non-blocking JavaScript timeline

Test example

As you can see the script file no longer blocks the downloads and the browser starts fetching the other components in parallel. And the overall response time is cut in half.

Dependencies

A problem with including scripts dynamically would be satisfying the dependencies. Imagine you’re downloading 3 scripts and three.js requires a function from one.js. How do you make sure this works?

Well, the simplest thing is to have only one file, this way not only avoiding the problem, but also improving performance by minimizing the number of HTTP requests (performance rule #1).

If you do need several files though, you can attach a listener to the script’s onload event (this will work in Firefox) and the onreadystatechange event (this will work in IE). Here’s a blog post that shows you how to do this. To be fully cross-browser compliant, you can do something else instead: just include a variable at the bottom of every script, as to signal “I’m ready”. This variable may very well be an array with elements for every script already included.

Using YUI Get utility

The YUI Get Utility makes it easy for you to use script includes. For example if you want to load 3 files, one.js, two.js and three.js, you can simply do:

var urls = ['one.js', 'two.js', 'three.js'];
YAHOO.util.Get.script(urls);

YUI Get also helps you with satisfying dependencies, by loading the scripts in order and also by letting you pass an onSuccess callback function which is executed when the last script is done loading. Similarly, you can pass an onFailure function to handle cases where scripts fail to load.

var myHandler = {
    onSuccess: function(){
        alert(':))');
    },
    onFailure: function(){
        alert(':((');
    }
};

var urls = ['1.js', '2.js', '3.js'];
YAHOO.util.Get.script(urls, myHandler);

Again, note that YUI Get will request the scripts in sequence, one after the other. This way you don’t download all the scripts in parallel, but still, the good part is that the scripts are not blocking the rest of the images and the other components on the page. Here’s a good example and tutorial on using YUI Get to load scripts.

YUI Get can also include stylesheets dynamically through the method
YAHOO.util.Get.css() [example].

Which brings us to the next question:

And what about stylesheets?

Stylesheets don’t block downloads in IE, but they do in Firefox. Applying the same technique of dynamic inserts solves the problem. You can create dynamic link tags like this:

var h = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.href = 'mycss.css';
link.type = 'text/css';
link.rel = 'stylesheet';
h.appendChild(link);

This will improve the loading time in Firefox significantly, while not affecting the loading time in IE.

Another positive side effect of the dynamic stylesheets (in FF) is that it helps with the progressive rendering. Usually both browsers will wait and show blank screen until the very last piece of stylesheet information is downloaded, and only then they’ll start rendering. This behavior saves them the potential work of re-rendering when new stylesheet rules come down the wire. With dynamic <link>s this is not happening in Firefox, it will render without waiting for all the styles and then re-render once they arrive. IE will behave as usual and wait.

But before you go ahead and implement dynamic <link> tags, consider the violation of the rule of separation of concerns: your page formatting (CSS) will be dependent on behavior (JS). In addition, this problem is going to be addressed in future Firefox versions.

Other ways?

There are other ways to achieve the non-blocking scripts behavior, but they all have their drawbacks.

Method Drawback
Using defer attribute of the script tag IE-only, unreliable even there
Using document.write() to write a script tag
  1. Non-blocking behavior is in IE-only
  2. document.write is not a recommended coding practice
XMLHttpRequest to get the source then execute with eval().
  1. eval() is evil”
  2. same-domain policy restriction
XHR request to get the source, create a new script tag and set its content
  1. more complex
  2. same-domain policy
Load script in an iframe
  1. complex
  2. iframe overhead
  3. same-domain policy

Future

Safari and IE8 are already changing the way scripts are getting loaded. Their idea is to download the scripts in parallel, but execute them in the sequence they’re found on the page. It’s likely that one day this blocking problem will become negligible, because only a few users will be using IE7 or lower and FF3 or lower. Until then, a dynamic script tag is an easy way around the problem.

Summary

  • Scripts block downloads in FF and IE browsers and this makes your pages load slower.
  • An easy solution is to use dynamic <script> tags and prevent blocking.
  • YUI Get Utility makes it easier to do script and style includes and manage dependencies.
  • You can use dynamic <link> tags too, but consider the separation of concerns first.


Read 6 times

Suscribing to  please login first
Non-blocking JavaScript Downloads -  Tech Weblogs - WEEKLYBITS.COM  Blogger Is a collection of i

Photologs

Yahoo User Interface Blog Blogger

Is a collection of industrial-grade JavaScript utilities and widgets that enable you to efficiently get the most out of today’s powerful web applications. Yahoo! User Interface Blog News and Articles about Designing and Developing wiht Yahoo! Libraries.

Non-blocking JavaScript Downloads
Stoyan Stefanov. - Non-blocking JavaScript Downloads About the Author: Stoyan Stefanov is a Yahoo! web developer working for the Exceptional Performance team and leading the development of the YSlow performance tool. He also an open-source contributor, conference speaker and technical writer: his latest book is called Object-Oriented JavaScript. External JavaScript files block downloads and hurt your page performance, but there is an [...] [..] Read complete article
Subscribe to Non-blocking JavaScript Downloads
Published 22-Jul-2008 by Stoyan Stefanov in DevelopmentPerformance
Read 6 times. More hits in More articles Non-blocking JavaScript Downloads Images about Non-blocking JavaScript Downloads
Nerdblog Blogger

Provides news about hardware, software, notebooks, laptops, PCs, Mac, PDAs
Nerdblog.Net

JavaScript 2's new direction (InfoWorld)
InfoWorld - Standardization efforts for the next version of JavaScript have taken a sharp turn this month, with some key changes in the Web scripting technology's direction. JavaScript creator Brendan Eich, CTO of Mozilla, has helped forge a consensus on how to proceed with the direction for JavaScript's improvements. “JavaScript was sitting still. It was [...] [..] Read complete article
Subscribe to Non-blocking JavaScript Downloads
Published 26-Aug-2008 by Nerdblog in General
Read 4 times. More hits in More articles JavaScript 2's new direction 
    (InfoWorld) Images about JavaScript 2's new direction 
    (InfoWorld)
Yahoo User Interface Blog Blogger

Is a collection of industrial-grade JavaScript utilities and widgets that enable you to efficiently get the most out of today’s powerful web applications. Yahoo! User Interface Blog News and Articles about Designing and Developing wiht Yahoo! Libraries.

Getting Started with JavaScript Unit Testing and YUI Test
For a long time, the web has been a wild west of technology. It’s only been within the past five years that any sort of rigor has been applied to web development and technologies such as HTML, CSS and JavaScript. JavaScript development has been the most affected, bringing discipline from other types of programming into [...] [..] Read complete article
Subscribe to Non-blocking JavaScript Downloads
Published 01-Dec-2008 by Nicholas C. Zakas in Development
Read 0 times. More hits in More articles Getting Started with JavaScript Unit Testing and YUI Test Images about Getting Started with JavaScript Unit Testing and YUI Test
Yahoo User Interface Blog Blogger

Is a collection of industrial-grade JavaScript utilities and widgets that enable you to efficiently get the most out of today’s powerful web applications. Yahoo! User Interface Blog News and Articles about Designing and Developing wiht Yahoo! Libraries.

Free Chapter from Douglas Crockford?s ?JavaScript: The Good Parts?
Click through to read Ch. 5 of Douglas The #2 book on Amazon’s JavaScript shelf this morning is Douglas Crockford’s forthcoming Javascript: The Good Parts (currently available for preorder and due for release later this month). In this volume, Douglas articulates a philosophy of coding in JavaScript that builds up on and extends the content of his popular video series and articles. [...] [..] Read complete article
Subscribe to Non-blocking JavaScript Downloads
Published 05-May-2008 by Eric Miraglia in Development
Read 13 times. More hits in More articles Free Chapter from Douglas Crockford?s ?JavaScript: The Good Parts? Images about Free Chapter from Douglas Crockford?s ?JavaScript: The Good Parts?
Nerdblog Blogger

Provides news about hardware, software, notebooks, laptops, PCs, Mac, PDAs
Nerdblog.Net

FCC set to punish Comcast on P2P blocking (CNET)
CNET - The Federal Communications Commission appears poised to take steps to punish Comcast for allegedly blocking access to file-sharing traffic. YahooTechNews [..] Read complete article
Subscribe to Non-blocking JavaScript Downloads
Published 27-Jul-2008 by Nerdblog in General
Read 5 times. More hits in More articles FCC set to punish Comcast on P2P blocking 
    (CNET) Images about FCC set to punish Comcast on P2P blocking 
    (CNET)
Nerdblog Blogger

Provides news about hardware, software, notebooks, laptops, PCs, Mac, PDAs
Nerdblog.Net

FCC poised to punish Comcast for traffic blocking (AP)
AP - A majority of members of the Federal Communications Commission have cast votes in favor of punishing Comcast Corp. for blocking subscribers' Internet traffic, an agency official said Friday. YahooTechNews [..] Read complete article
Subscribe to Non-blocking JavaScript Downloads
Published 26-Jul-2008 by Nerdblog in General
Read 4 times. More hits in More articles FCC poised to punish Comcast for traffic blocking 
    (AP) Images about FCC poised to punish Comcast for traffic blocking 
    (AP)
Nerdblog Blogger

Provides news about hardware, software, notebooks, laptops, PCs, Mac, PDAs
Nerdblog.Net

Facebook Says China Not Blocking Access (PC World)
PC World - Facebook says China is not blocking its Web site, despite reports of problems. YahooTechNews [..] Read complete article
Subscribe to Non-blocking JavaScript Downloads
Published 02-Jul-2008 by Nerdblog in General
Read 6 times. More hits in More articles Facebook Says China Not Blocking Access 
    (PC World) Images about Facebook Says China Not Blocking Access 
    (PC World)
Kotaku`s The Gamers Guide Blogger

XBOX 360 Gamers Weblog Gossip, news and leaks for obsessive gamers Kotaku As if you don't waste enough of your time in a gamer's haze, here's Kotaku: a gamer's guide that goes beyond the press release. Gossip, cheats, criticism, design, nostalgia, pred

E3 Gate Crasher: Ghetto Vader - Let Him In? [E308]
Ghetto Vader here came all the way from The Bronx to crash E3 even though he doesn't have an invite. Should we let him in? Gawker Media polls require Javascript; if you're viewing this in an RSS reader, click through to view in your Javascript-enabled web browser. [..] Read complete article
Subscribe to Non-blocking JavaScript Downloads
Published 16-Jul-2008 by Noah R in e308 Cosplay E3 08 Gatecrasher Ghetto vader Project 420 Rap
Read 6 times. More hits in More articles  E3 Gate Crasher: Ghetto Vader - Let Him In? [E308] Images about  E3 Gate Crasher: Ghetto Vader - Let Him In? [E308]
Kotaku`s The Gamers Guide Blogger

XBOX 360 Gamers Weblog Gossip, news and leaks for obsessive gamers Kotaku As if you don't waste enough of your time in a gamer's haze, here's Kotaku: a gamer's guide that goes beyond the press release. Gossip, cheats, criticism, design, nostalgia, pred

Super Mario Bros. In 14 Kilobytes Of Javascript [Nintendo]
It may be lacking in Koopas, power ups and an underworld—not to mention the dreaded embedded MIDI soundtrack—but this Super Mario Bros. clone packed into 14 KB of Javascript is still [..] Read complete article
Subscribe to Non-blocking JavaScript Downloads
Published 09-Apr-2008 by Michael McWhertor in NesNintendoRetroSuper Mario Bros.
Read 10 times. More hits in More articles Super Mario Bros. In 14 Kilobytes Of Javascript [Nintendo] Images about Super Mario Bros. In 14 Kilobytes Of Javascript [Nintendo]
Yahoo User Interface Blog Blogger

Is a collection of industrial-grade JavaScript utilities and widgets that enable you to efficiently get the most out of today’s powerful web applications. Yahoo! User Interface Blog News and Articles about Designing and Developing wiht Yahoo! Libraries.

Notoptimal Dev?s New YUI-Based Accordion Menu
Visit Notoptimal Dev Juan I. Leon at Notoptimal Dev went looking for the perfect Accordion Menu recently, but the search was not a success. The criteria: be lightweight use unobtrusive Javascript techniques (ie not have scattered Javascript all over the markup) use simple CSS to make it look nice needed to support both single and multiple sections opened at [...] [..] Read complete article
Subscribe to Non-blocking JavaScript Downloads
Published 07-Jun-2008 by Eric Miraglia in Development
Read 10 times. More hits in More articles Notoptimal Dev?s New YUI-Based Accordion Menu Images about Notoptimal Dev?s New YUI-Based Accordion Menu
Yahoo User Interface Blog Blogger

Is a collection of industrial-grade JavaScript utilities and widgets that enable you to efficiently get the most out of today’s powerful web applications. Yahoo! User Interface Blog News and Articles about Designing and Developing wiht Yahoo! Libraries.

Production JavaScript Debugging
Setting up Firefox You know the scenario. A bug is filed for a JavaScript issue in production. You update your development server to the same files (allegedly) that are in production but you can’t reproduce the issue. Debugging your JavaScript code is horrifically difficult, if not impossible, because you’re following best practices and crunching the file using the YUI Compressor. You start [...] [..] Read complete article
Subscribe to Non-blocking JavaScript Downloads
Published 27-Jun-2008 by Nicholas C. Zakas in Development
Read 13 times. More hits in More articles Production JavaScript Debugging Images about Production JavaScript Debugging
Kotaku`s The Gamers Guide Blogger

XBOX 360 Gamers Weblog Gossip, news and leaks for obsessive gamers Kotaku As if you don't waste enough of your time in a gamer's haze, here's Kotaku: a gamer's guide that goes beyond the press release. Gossip, cheats, criticism, design, nostalgia, pred

PS3 Ad Spills Beans On TV Show Downloads? [PS3 TV Downloads]
Certainly appears so. There's a new PS3 ad doing the rounds, one which shows off each of the console's main features. One of the features shown is the PlayStation Store, and in particular the kind of stuff you can downlo [..] Read complete article
Subscribe to Non-blocking JavaScript Downloads
Published 12-May-2008 by Luke Plunkett in NewsPlaystation 3playstation storePS3PS3 TV DownloadsRumorSonyTop
Read 10 times. More hits in More articles PS3 Ad Spills Beans On TV Show Downloads? [PS3 TV Downloads] Images about PS3 Ad Spills Beans On TV Show Downloads? [PS3 TV Downloads]

Warning We are not responsible of information posted from external feeds. Use this website at your own risk. Notice: We will not be liable for any direct or indirect loss or damage arising under this disclaimer or in connection with our website, whether arising in tort, contract, or otherwise.


Your Site here Your Site here Your site here Your site here Your site here