JavaScript;在脚本中调用 src ?

发布于 2024-10-10 20:43:05 字数 843 浏览 2 评论 0原文

我正在制作一个简单的联属网络营销广告轮换 JavaScript。我对 JavaScript 还很陌生,并不完全理解它,所以请耐心等待。

Google 的 adsense 分为两部分,一部分用于设置变量,另一部分用于获取脚本。然后亚马逊有一个 iframe 来获取它的广告。我想做的就是使用 1 到 2 之间的随机数(稍后会更大),它将随机选择其中一个来显示在我的本地主机上。

<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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍风燃霜 2024-10-17 20:43:05

您需要使用 document.write("or text 将 html 写入页面,但对于 iframe,我建议将其放入另一个 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("<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>
兲鉂ぱ嘚淚 2024-10-17 20:43:05

您需要使用 document.write("stringtowritetodocument"); 才能让 JavaScript 将任何内容写入文档。

因此,在您的 if 内部:

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

此外,一旦复杂性增加,您可能需要注意这一点:
JavaScript的document.write内联脚本执行顺序

你想要的也可能是如果您可以使用一些服务器端代码,则可以更好地完成任务。

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.

百善笑为先 2024-10-17 20:43:05

您尝试过Document.Write()吗?

例如 Document.Write("

此处为您的 HTML

Have you tried Document.Write()?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文