简体   繁体   English

将JavaScript广告代码放置在页面中的其他位置

[英]Placing JavaScript ad tag on different location in page

I have a page, that calls "first.js" script. 我有一个页面,该页面调用“ first.js”脚本。 This script ads to a call to "second.js" script: 该脚本可用于调用“ second.js”脚本:

first.js: first.js:

var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type= 'text/javascript';
script.src= 'second.js';
document.getElementsByTagName("head")[0].appendChild(script)

now, if I put in second.js alert("test"), I will see a good alert. 现在,如果我输入second.js alert(“ test”),我会看到一个很好的警报。 However, if I put in second.js document.write("something"), I will not get that document.write. 但是,如果我输入second.js document.write(“ something”),则不会获得该document.write。

Any idea why? 知道为什么吗?

Because it is being loaded into the head , typically where none of the elements are rendered in the browser's viewport. 因为它被加载到head ,通常在浏览器的视口中没有任何元素被渲染。

If this were to run too, it would clear the page if had started to render as document.write() implicitly calls document.open() first. 如果这也可以运行,它将清除页面,如果它已开始呈现为document.write()隐式调用document.open()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM