Jump to content

javascript trick: Loading remote javascript after pageload with onload


Leon

Recommended Posts

  • Administrators

Hi,

 

So, malin (my wife) had a problem today with a remote tracker script from www.susnet.se dident work because their server is down, this caused her blog to load extremely slow.

 

The solution was quite simple, instead of loading the susnet code in the middle of her site, we would load the susnet code AFTER the page have loaded using a OnLoad event.

 

The change look like this.

 

1. Change

to

2. Create a new javascript at the end of the page like this

 

<script>
function doneLoading() {
loadSusnet();
}
</script>

 

The above function will call yet another function called "loadSusnet"

 

3. Create the LoadSusnet javascript function and the end of the code.. like this....

 

<script>
function loadSusnet() {
var scriptSusnet1 = document.createElement('script');
scriptSusnet1.type	= 'text/javascript';
scriptSusnet1.src	= 'http://susnet.se/susnetstat.js';
document.getElementsByTagName('head')[0].appendChild(scriptSusnet1);
susnet_counter_id = 60529;
//susnet_security_code = '76388'
susnet_node=0;
register();

var scriptSusnet2 = document.createElement('script');
scriptSusnet2.type	= 'text/javascript';
scriptSusnet2.src = 'http://susnet.se/susnetstat.js';
document.getElementsByTagName('head')[0].appendChild(scriptSusnet2);
susnet_counter_id = 60529;
//susnet_security_code = '76388'
susnet_node=0;
getTotalUniqueVisitors();
}
</script>

 

What the susnet function does is to dynamically load the remote javascript files, all this is done AFTER the page have rendered, hence the page now loads ALOT faster... - offcourse, this should be used wisefully.

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...