kami
kami

Reputation: 1

load script last

I have a webpage which lags while loading, because an script is trying to be processed. The scripts creates an iframe for an add service. My webpage load sequentially, but the script delays the entire page because it appears at the top...

Is it possible to have the script load last, but still have it appear at the top of the page?

Edit: I have tried this, but it doesn't work:

<script type="text/JavaScript">
  window.onlaod=function(){
  var root = document.body.getElementById('top-banner');
  var oJs = document.createElement('script');
  oJs.setAttribute('type','text/JavaScript');
  oJs.setAttribute('src','http://anetwork.ir/showad/?adwidth=468&adheight=60&aduser=1341223032');
  root.appendChild(oJs);
  }
</script>

top-banner is the ID for div i want to show the ad. it is empty.

Upvotes: 0

Views: 4218

Answers (3)

Al-Punk
Al-Punk

Reputation: 3659

If you want a specific JS code to load last:

if (typeof jQuery !== 'undefined') {
    (function($) {
        //use window.bind to load the tinymce per last. Otherwise will throw error
        $(window).bind("load", function() {
           // code here will load after page is loaded
        });
    })(jQuery);
}

Upvotes: 0

Nuxy
Nuxy

Reputation: 384

If you have problems on the load of a script because it's execution you should use functions to call that script, so, if you have a function for the script or it's automatic, you should put the script causing problems inside of a function.

When that's done you could add the onload property in the body tag and then call the function that creates the iframe:

<body onload="createIframe()">...</body>

Like this your script will be loaded just when the body has finished loading.

Edit:

Modify your script like that and keep the body onload showed before:

<script type="text/JavaScript">
function createIframe(){
  var root = document.getElementById('top-banner');
  var oJs = document.createElement('script');
  oJs.setAttribute('type','text/JavaScript');
  oJs.setAttribute('src','http://anetwork.ir/showad/?adwidth=468&adheight=60&aduser=1341223032');
  root.appendChild(oJs);
}
</script>

Upvotes: 1

Jes&#250;s
Jes&#250;s

Reputation: 780

You need to move the script tag to the end of the page (just after the </body> tag, for example).If you wanna modify previous elements of your page in function of the javascript code, you need to make it using javascript code.

That is: If you wanna load data from the server, for example, and show it formatted, you must show the HTML and then, when the script and the data is loaded, "parse" the results and "insert it" in the HTML using javascript functions.

Upvotes: 0

Related Questions