Search code examples
javascripthtmlajaxsrc

Javascript embed code that doesn't halt page rendering


I'd like to embed a number from another page. The remote call is small (only returns a number) but I would like the page to keep loading while the request is out. How should I do it? I'm currently doing

<span id="target">Loading ...</span>
<script>
var cb = function(data) {
    document.getElementById("target").innerHTML = data; 
}
</script>
<script src="http://webnumbr.com/webnumbrs.latest.json(callback=cb)"></script>

I'm open to client side or server side changes. Just the least code for the client embed the better


Solution

  • The "official" recommendation (see rules) is to put all JavaScript at the very bottom of the HTML file. The contents of every tag will be evaluated before rendering continues, as the outcome of the script might affect further rendering (think of document.write()).