Wednesday, November 17, 2010

Delay postpone loading JavaScript execution in order to solve slow down problem

Delay postpone loading JavaScript execution in order to solve slow down problem

Method 1:

<body onload="doWhatever();">

Method 2:

<html>
<head>
<title>My Web Page</title>
<script type="text/javascript">
function LoadTrigger()
{
alert('This will fire after the page has finished loading.');
}
window.onload = LoadTrigger;
</script>
</head>
<body>
<p>
My Web Page is great!
</p>
</body>
</html>

Method 3:

<html>
<head>
<title>My Web Page</title>
<script language="javascript">
function LoadTrigger(){
document.getElementById("AdServer").innerHTML = "<iframe src='http://example.com/test.js' width='728' marginwidth='0' frameborder='0'></iframe>";
}
window.onload = LoadTrigger;
</script>
</head>
<body>
<!-- Placeholder for the iframe -->
<div id="AdServer"></div>
</body>
</html>

Method 4:

setTimer('functionFoo()',10);

Method 5:

<script ...>
<!--
function stub(){};
// -->
</script>
</head>
<body>
...
<script src="/scripts/stub.js" type="text/javascript"></script>
</body>The empty stub function(s) allow users to interact with your page without generated "undefined" JavaScript errors. The scripts loaded just before the closing body tag redefine the stub function once the body content has displayed.

Be careful with this approach, however. Large JavaScript files, especially those that execute slowly, can bog down the response time of your page after it has loaded. Slow post-load response is more harmful to user satisfaction than slow page load times, according to current HCI research.

Reference:
http://www.daniweb.com/forums/thread40148.html
http://www.websiteoptimization.com/speed/tweak/delay/

No comments: