This Synchronous or blocking script execution is the default only. The <script> tag can have defer and async attributes, which cause scripts to be executed differently. This makes your website loading much faster than before and appears contents of your site by loading at first.
Loading JavaScript Asynchronously using async or defer attributes
Both the defer and acync attributes are ways of telling the browser that the linked script does not use document.write() and won't be generating document content, and that therefore the browser can continue to parse and render the document while downloading the script. You can use async or defer attributes as the following.
<script defer src="deferred.js"></script>
<script async src="async.js"></script>
Here is an example of acync script uses in this blog for Intensedebate comments script source.
<script async='async' expr:src='data:post.commentSrc' type='text/javascript'/>
Loading JavaScript Asynchronously by loading scripts dynamically
You can load and execute scripts asynchronously, even in browsers that do not support the async attribute, by dynamically creating a <script> element and inserting it into the document. Here is an example how to load scripts dynamically.
function loadasync(url){
var head=document.getElementByTagName("head")[0];
var s=document.createElement("script");
s.src=url;
head.appendchild(s);
}
You can use the following method to execute loadsync() function, when document finished loading.
function loadasync(){ ..................}
request.onreadystatechange=loadasync;
<div style='display:none'> <div id='adsource-0'>
<script type='text/javascript'>
var infolinks_pid = 9993182;
var infolinks_wsid = 1;
</script>
<script language='javascript' src='http://resources.infolinks.com/js/infolinks_main.js' type='text/javascript'/>
</div>
</div>
<script type='text/javascript'>
source = document.getElementById("adsource-0");
placeholder = document.getElementById("ad-0");
placeholder.appendChild(source);
</script>
I have placed this code at the bottom of HTML codes, i.e. just before </body> tag and have placed the following code where I want to display ads.
<div id="ad-0" align="center"></div>
Loading JavaScript Asynchronously when document finishes loading
You can load and execute scripts asynchronously by using setTimeout(), addEventListner(), and attachEvent(). Most objects that can be event targets have a method named addEventListner(), which allows the registration of multiple listeners.
window.addEventListner("load", function(){.....},false);
request.addEventListner("readystatechange", function(){......},false);
The first argument to this function is the name of the event. For IE8 and earlier, you must use a similar method, named attachEvent().
window.attachevent("onload", function() {.....});
Here is an example which define an onLoad() function that registers a function to be run when the document finishes loading. If the document has already loaded, run it asynchronously.
function onLoad(f){
if(onLoad.loaded)
window.setTimeout(f,0);
elseif (window.addEventListner)
window.addEventListner("load",f,false);
elseif (window.attachEvent)
window.attachEvent("onload",f);
}
onLoad.loaded=false;
onLoad(function(){onLoad.loaded=true;});
Related Posts:
- How To Create Simple Image Slideshow Using JavaScript ?
- Image Slideshow with Navigation Buttons Using JavaScript
- How to Create JavaScript Image Slideshow with LInks
- How to Display Date Format in JavaScript?
- How to Create a Digital Clock in JavaScript?
- What are the Different Ways to Redirect Page in JavaScript?
- How to Detect Visitor's Browser Using JavaScript?
إرسال تعليق
Click to see the code!
To insert emoticon you must added at least one space before the code.