javascript - Waiting for jquery to load asynchronously before loading js -
i using following load ads on site
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script type="text/javascript"> var adwidth = $(document).width(); google_ad_client = "ca-pub-6777348526535979"; if ( adwidth >= 768 ) { google_ad_slot = "3870513647"; google_ad_width = 728; google_ad_height = 90; } else { google_ad_slot = "1127560842"; google_ad_width = 320; google_ad_height = 50; } </script> <script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js"></script>
it works fine think can optimize ad delivery further.i want load jquery asynchronously , following script must wait jquery loaded.
<script type="text/javascript"> var adwidth = $(document).width(); google_ad_client = "ca-pub-6777348526535979"; if ( adwidth >= 768 ) { google_ad_slot = "3870513647"; google_ad_width = 728; google_ad_height = 90; } else { google_ad_slot = "1127560842"; google_ad_width = 320; google_ad_height = 50; } </script> <script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js"></script>
how can accomplish ?
you can way:
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script type="text/javascript"> var google_ad_slot; var google_ad_width; var google_ad_height; var google_ad_client; $(document).ready(function() { var adwidth = $(document).width(); google_ad_client = "ca-pub-6777348526535979"; if ( adwidth >= 768 ) { google_ad_slot = "3870513647"; google_ad_width = 728; google_ad_height = 90; } else { google_ad_slot = "1127560842"; google_ad_width = 320; google_ad_height = 50; } $("head").append('<script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js"></script>'); }); </script>
after dom ready , scripts (including jquery) loaded, jquery call $(document).ready()
function, values initiated , google ad script added head
section of document.
all modern browsers correctly load , run script after adding dom.
variables should global let google script work them. can try changing $(document).ready
$(window).load
if won't work become absolutely sure has been loaded.
Comments
Post a Comment