1

I'm making a simple affiliate ad rotation JavaScript. I'm still new to JavaScript and don't fully understand it so bear with me.

Google's adsense is split into 2 parts, one to set the variables, and the next to get the script. Then Amazon has an iframe to get it's ad's. All I want to do is use a random number from 1 to 2 (will be larger later) that will randomly select one of them to display on my localhost.

<script type="text/javascript"><!--
/* Custom footer */

select = rand(2);

if(select == 1){
     google_ad_client = "-----------";
     google_ad_slot = "---------";
     google_ad_width = ---;
     google_ad_height = --;

     //get this google 
     <script ...src="http://pagead2.googlesyndication.com/pagead/show_ads.js

} else {
         <iframe src="http://rcm.amazon.com/e/cm?t=------&o=1&p=48&l=ur1&category=amazonhomepage&f=ifr" width="728" height="90" scrolling="no" border="0" marginwidth="0" style="border:none;" frameborder="0"></iframe>
}
</script>
3
  • I'm not quite sure what you're asking... Commented Jan 6, 2011 at 17:47
  • First make sure what you are trying to do doesn't break any adsense policies. Any modification to their generated script code is in violation with their policy. Commented Jan 6, 2011 at 17:49
  • Breaking their policy? I thought that meant modifying the data from their show_ads.js Commented Jan 6, 2011 at 20:16

3 Answers 3

2

You need to use document.write("<html>or text</html") to write html out to the page, though for the iframe i would suggest putting it inside another div

<script type="text/javascript"><!--
/* Custom footer */

var select = Math.floor(Math.random()*2);

if(select == 1){
     google_ad_client = "-----------";
     google_ad_slot = "---------";
     google_ad_width = ---;
     google_ad_height = --;

     //get this google 
     document.write("<script ...src='http://pagead2.googlesyndication.com/pagead/show_ads.js' />");

} else {
     document.getElementById('adContainer').innerHTML('<iframe src="http://rcm.amazon.com/e/cm?t=------&o=1&p=48&l=ur1&category=amazonhomepage&f=ifr" width="728" height="90" scrolling="no" border="0" marginwidth="0" style="border:none;" frameborder="0"></iframe>');
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

2

You need to use document.write("stringtowritetodocument"); in order to get JavaScript to write anything to the document.

So, inside your if:

document.write('<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>');

Also, once this grows in complexity, you may need to look out for this: JavaScript's document.write Inline Script Execution Order

What you want may also be accomplished better with some server-side code, if that is available to you.

Comments

0

Have you tried Document.Write()?

e.g. Document.Write("<p>Your HTML Here</p");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.