我正在尝试一种新的广告服务,据我所知,他们没有提供加载广告的功能界面。我们希望根据用户的屏幕尺寸展示不同的广告尺寸,而该服务需要您加载不同的广告尺寸.js
每种尺寸的 URL。
我最初尝试写:
<script type="text/javascript"><!--
var dochead = document.getElementsByTagName('head')[0];
var newscript = document.createElement('script');
newscript.type = "text/javascript";
newscript.src = '//ads-by.madadsmedia.com/tags/22430/9194/async/' + (screen.width >= 1360 ? '160' : '120') + 'x600.js';
dochead.appendChild(newscript);
//-->
</script>
但我只有一张空白页。我查看了 Chrome 开发者工具,它似乎正确加载了他们的脚本。他们的脚本加载来自 Google 的其他脚本,并且它们也出现在 DOM 中。但没有广告图片。
当我将脚本更改为:
<script language="JavaScript" type="text/javascript">
var prot = document.location.protocol;
var adwidth = (screen.width >= 1360 ? '160' : '120');
document.write('<script language="JavaScript" type="text/javascript"'); document.write('src="'+prot+'//ads-by.madadsmedia.com/tags/22430/9194/async/'+adwidth+'x600.js">'); document.write('<\/scr' + 'ipt>');
</script>
它工作正常。我一般不喜欢使用document.write
,我想知道为什么在这种情况下需要它?广告服务的脚本广泛使用document.write
,是因为这个吗?
Because they正在使用document.write()
:
http://ads-by.madadsmedia.com/tags/22430/9194/async/160x600.js http://ads-by.madadsmedia.com/tags/22430/9194/async/160x600.js:
if (!window.ActiveXObject){
document.write("<div style=\"text-align: center; margin: 0px auto; width:160px; height:600px; position:relative;\">");
// etc.
If document.write()
不是在线运行并主动“打开”文档,它会破坏那里的内容。因此,加载后运行他们的脚本会用他们的内容覆盖您的内容。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)