Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject external javascript file in html body after page load

I want to load an external javascript after page is loaded. Actually the javascript contains source for an ad and its making page load slow. All I want is to delay loading & execution of ads to make sure fast page load.

thanks, Bilal

like image 519
Tepu Avatar asked May 24 '12 09:05

Tepu


2 Answers

You may just use this script at the last tag of your body block:

<script type="text/javascript">
   var script = document.createElement('script');
   script.setAttribute('src', 'http://yourdomian.com/your_script.js');
   script.setAttribute('type', 'text/javascript');
   document.getElementsByTagName('head')[0].appendChild(script);
</script>
like image 194
Aleš Kotnik Avatar answered Nov 15 '22 18:11

Aleš Kotnik


var script=document.createElement('script');
script.type='text/javascript';
script.src=url;

$("body").append(script);

Courtsey

like image 34
Ankur Verma Avatar answered Nov 15 '22 16:11

Ankur Verma