Current time: 03-06-2021, 11:34 AM Hello There, Guest! (LoginRegister)

Post Reply 
 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to defer parsing javascript
11-22-2011, 07:27 AM
Post: #7
RE: how to defer parsing javascript
To defer javascript, I use the following code. It works for all browsers, is compatible with everything (as far as I'm aware). Just add the following code right before closing body tag.

Code:
<script defer="defer" type="text/javascript">
    window.onload=function(){
        var mycode;
        mycode=document.createElement("script");
        mycode.type="text/javascript";
        mycode.src="http://www.example.com/path/yourjavascriptfile.js";
        document.getElementsByTagName("head")[0].appendChild(mycode);
    }
</script>

</body>

Note that this is code by me. Google suggests the following syntax:

Code:
<script type="text/javascript">

// Add a script element as a child of the body
function downloadJSAtOnload() {
var element = document.createElement("script");
element.src = "deferredfunctions.js";
document.body.appendChild(element);
}

// Check for browser support of event handling capability
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else window.onload = downloadJSAtOnload;

</script>

... as shown here:
http://code.google.com/speed/page-speed/...rLoadingJS


If everything is implemented correctly, then the result is nice. As shown here:
http://www.webpagetest.org/result/111121...1/details/

Note that javascript file is downloaded ONLY after "document complete" event! Win!

Hope this helps Smile
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
how to defer parsing javascript - Joel - 11-15-2011, 04:47 AM
RE: how to defer parsing javascript - Joel - 11-15-2011, 06:44 AM
RE: how to defer parsing javascript - Joel - 11-15-2011, 07:35 AM
RE: how to defer parsing javascript - laboot - 11-22-2011 07:27 AM
RE: how to defer parsing javascript - Joel - 11-22-2011, 10:25 AM

Forum Jump:


User(s) browsing this thread: 1 Guest(s)