How to load some javascript after Page load?

How to load some javascript after Page load?

I want some external javascripts to be loaded after 5 seconds the page loaded, like for Google Analytics and a few more etc. etc…
Thanks.

<html>
<body onload=“myfunction()”>
<p id=“text”></p>
</body>
</html>

//in your external js file:

function myfunction(){
var t=setTimeout(“newfunction()”,5000);
}

function newfunction(){
alert(“5 seconds later…”);
document.getElementById(‘text’).innerHTML = “hello world!”;
}

But Sir, how the page knows where the external file is?

I have

This code is to be called after 5 seconds,

<script src=“http://abx.com/123.js” type=“text/javascript”></script>
<script type=“text/javascript”>
var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-5472515-3’]);
_gaq.push([‘_setDomainName’, ‘none’]);
_gaq.push([‘_setAllowLinker’, true]);
_gaq.push([‘_trackPageview’]);
(function() {
var ga = document.createElement(‘script’); ga.type = ‘text/javascript’; ga.async = true;
ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘http://www’) + ‘.google-analytics.com/ga.js’;
var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);
})();
</script>

How to do this?

replace this:

function newfunction(){
alert(“5 seconds later…”);
document.getElementById(‘text’).innerHTML = “hello world!”;
}

with this:
function newfunction(){
var _gaq = _gaq || ;
_gaq.push([‘_setAccount’, ‘UA-5472515-3’]);
_gaq.push([‘_setDomainName’, ‘none’]);
_gaq.push([‘_setAllowLinker’, true]);
_gaq.push([‘_trackPageview’]);
(function() {
var ga = document.createElement(‘script’); ga.type = ‘text/javascript’; ga.async = true;
ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘http://www’) + ‘.google-analytics.com/ga.js’;
var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);
})();
}

tell me if this works?

Thanks sir,

But I am really sorry to disturb you again, because I am new to javascript.

Sir, The above file from the code which I told (which is not controllable to me because it is on other server).

<script src=“http://abx.com/123.js” type=“text/javascript”></script>

What to do with this sir?
I only want the delayed loading to load external JS files which are not controllable to me ( If it were on my server, then of course, I can put their text in a new JavaScript function… But it is not)

this should work:

remove this line from your document-<script src=“http://abx.com/123.js” type=“text/javascript”></script>

<html>
<head>
<script type="text/javascript">
function myfunction(){
var t=setTimeout("newfunction()",5000);
}

function newfunction(){
var mysrc=document.createElement('script');
mysrc.src="http://abx.com/123.js";
document.getElementsByTagName('head')[0].appendChild(mysrc); 
}
</script>
</head>
<body onload="myfunction()">
</body>
</html>

^ when page loads, function will be called which will call another function after 5 seconds, this function will then add "<script src=“http://abx.com/123.js” type=“text/javascript”></script> " to the document. So this should work…