How to combine embedded JavaScripts into one?

I am fixing a template that we use on one of our sites, which has the following code

This snippet works.

<script type="text/javascript" src="http://partner.googleadservices.com/gampad/google_service.js"></script> <script type="text/javascript"> GS_googleAddAdSenseService("ca-pub-123"); GS_googleEnableAllServices(); </script> <script type="text/javascript"> GA_googleAddSlot("ca-pub-123", "Foo"); GA_googleAddSlot("ca-pub-123", "Bar"); </script> <script type="text/javascript"> GA_googleFetchAds(); </script> 

I tried combining static scripts like this

 <script type="text/javascript" src="http://partner.googleadservices.com/gampad/google_service.js"></script> <script type="text/javascript"> GS_googleAddAdSenseService("ca-pub-123"); GS_googleEnableAllServices(); GA_googleAddSlot("ca-pub-123", "Foo"); GA_googleAddSlot("ca-pub-123", "Bar"); GA_googleFetchAds(); </script> 

However, now I get an error

 Uncaught ReferenceError: GA_googleAddSlot is not defined 

I'm not a noob when it comes to JavaScript, but I can't imagine why a combination of three built-in scripts into a single <script> would make a difference here.

Any ideas?

+5
source share
2 answers

google_service.js does not define GA_googleAdSlot , but defines GS_googleEnableAllServices . When GS_googleEnableAllServices is GS_googleEnableAllServices , it uses document.write to insert a new script element that loads the GA_googleAdSlot definition. A new script element is inserted into the document after the end of the current script element. It is difficult, but it is your answer.

+3
source

Check it out: https://support.google.com/dfp_sb/answer/112649?hl=en

This is part of document support:

For small businesses, DFP requires separate JavaScript blocks, described below. Do not combine JavaScript blocks or your code may break.

They clearly mentioned that you should not combine Javascript blocks!

I'm not sure why, but as long as this is mentioned in the document, you must follow the rules.

+1
source

Source: https://habr.com/ru/post/1201375/


All Articles