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>
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>
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.
Have you tried Document.Write()
?
e.g. Document.Write("<p>Your HTML Here</p");
精彩评论