2

I am using the following to load ads on my 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 but i think i can optimize the ad delivery further.I want to load the jquery asynchronously and thus the following script must wait for the jquery to be 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 i accomplish this ?

2 Answers 2

3

You can do it this 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 will be ready and scripts (including jQuery) will be loaded, jQuery will call this $(document).ready() function, your values will be initiated and Google Ad script will be added to the head section of a document.

All modern browsers correctly load and run script after adding it to DOM.

Variables should be global to let Google script work with them. You can also try changing $(document).ready to $(window).load if it won't work to become absolutely sure it has been loaded.

Sign up to request clarification or add additional context in comments.

5 Comments

Will it work if there is async in script tag for jquery.min.js
@user2650277 It seems like it will not :( In case of async scripts, $(document).ready() fires after DOM is ready. Async scripts can be still not loaded.
Is there any way you can of where i can load all my scripts async so that it doesn't affect load time
@user2650277 You can add all scripts manually on $(document).ready() using $.append function or $.getScript. However, as I suppose, it is a bad practice. The best you can do it to load jQuery synchronously and load all other scripts asynchronously. I am sure it won't affect loading - jquery.min.js is a really light-weight library; moreover, it is static and it is properly cached by browser. jQuery 2.1.4 is being downloaded for 14 ms.
I think there is some issue with your code.Ad doesn't load and i see '); }); instead
2

I think what you're looking for is AMD. AMD (asynchronous module definition) is a specification that allows Javascript modules to explicitly declare their dependencies and load asynchronously.

Probably the most famous implementation of AMD is RequireJS. Basically, you move your code from the <script> tag and change it as follows:

require(['jquery'], function($){ var adWidth = $(document).width(); // And the rest of your code }); 

Beware though, that jQuery doesn't support AMD very well, so you'll have to jump through some hoops. There are instructions on the RequireJS website to handle jQuery.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.