Sometimes I have issues while loading some ad scripts. If the external server is not responding, the whole page waits for the respond and stops loading. How can I tell those problem scripts to start executing after:
- The whole page is loaded.
- All other scripts are loaded.
3 Answers 3
Using defer doesn't guarantee that it will defer both loading and running. You can control exact time when you want this script to start loading and executing by adding it to page dynamically by placing <script> with following contents after everything else you want to load and run:
var head=document.getElementsByTagName('head')[0]
var script=document.createElement('script')
script.setAttribute('type', 'text/javascript')
script.setAttribute('src', "http://your-script.com/address-here.js")
head.appendChild(script)
14 Comments
defer doesn't defer?.createTextNode and script.appendChild but even without looking in specs, I'm under impression that text child of script tag is silently ignored if you specify a src attribute.Use the DEFER attribute of javascript declaration:
<script src="script.js" type="text/javascript" defer="defer"></script>
Comments
You need to defer your script. This will allow this rest of the page to load whilst this script is downloading.
Here is how to do it:
<script src="yourscript.js" defer="defer" type="text/javascript"></script>
More reading - http://www.websiteoptimization.com/speed/tweak/defer/