AdSense Dynamic Modules

I have an OpenSocial app and want to try AdSense. I can easily run Javascript, but I cannot directly include HTML snippets. For this reason, I'm trying to create a script that would create a DIV containing an AdSense ad.

function adsense(w, h, slot) {
google_ad_client = "pub-4815352041522054";
google_ad_slot = slot;
google_ad_width = w;
google_ad_height = h;

var url = "http://pagead2.googlesyndication.com/pagead/show_ads.js";    
document.write(
    '<div style="border:solid 3px red;">'
    + '<sc' + 'ript src="' + url + '">'
    + '</sc' + 'ript>'
    + 'The ad should be inside the same box as this text.</div>'
  );
}

adsense(160, 600, 5133629129);

In the above code, write a div with a red border and place the AdSense module inside it. This works fine in Chrome, but in IE the specified AdSense code seems to run after the div is already closed.

I would like to be able to create AdSense modules dynamically from code (e.g. for testing A / B) and modify them as needed. Any ideas on how to make this work?

+3
source share
2

, , , StackOverflow ( ), , , iframe,

1
HTML AdSense, :

<script type="text/javascript"><!--
google_ad_client = "ca-pub-xxxxxxxxxxxx";
/* Some Ad */
google_ad_slot = "xxxxxxxxx";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

2

HTML , , , :

<style> body {padding:0;margin:0;} </style>

, iframe , iframe.

3
iframe :

var insertAdsenseIframe = function(src,width,height){
  var iframe = document.createElement('iframe');
  iframe.setAttribute("frameborder","0");
  iframe.setAttribute("src", src);
  iframe.setAttribute("width", width);
  iframe.setAttribute("height", height);
  return iframe; //Return it so you can use whatever method you want to insert/append
}

var myIframe = insertAdsenseIframe('http://oscargodson.com/dev/scriptTest/cleariframe.html','468px','60px');

document.getElementById('test').appendChild(myIframe);

JavaScript - , . IE - . , .

Demo

TOS, JSBin, StackOverflow div , , .

: http://jsbin.com/evoqi6/ Hack: http://jsbin.com/evoqi6/edit

+1

div , . , IE javascript..

<div style="border:solid 3px red">
<script type="text/javascript" charset="utf-8">

function adsense(w, h, slot) {
google_ad_client = "pub-4815352041522054";
google_ad_slot = slot;
google_ad_width = w;
google_ad_height = h;

var url = "http://pagead2.googlesyndication.com/pagead/show_ads.js";    
document.write(
     '<sc' + 'ript src="' + url + '">'
    + '</sc' + 'ript>'
    + 'The ad should be inside the same box as this text.'
  );
}

adsense(160, 600, 5133629129);


</script>
</div>
0

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


All Articles