简体   繁体   English

iframe与document.write

[英]iframe with document.write

I need to insert the ad code with the document.write. 我需要在document.write中插入广告代码。 But it overwrites the entire page. 但是它会覆盖整个页面。 The script has the following form: 该脚本具有以下形式:

<script>
document.write.// some code
</script>

Tell me, please, how best to insert it? 请告诉我,如何最好插入? I think to use iframe, but maybe there are better methods. 我想使用iframe,但也许有更好的方法。 It possible to write it inside the iframe, like: <iframe><script>somecode</script></iframe> ? 可以将其写入iframe中,例如: <iframe><script>somecode</script></iframe>吗? We use AdFox, so the script should be without any libraries. 我们使用AdFox,因此脚本应该没有任何库。

Thanks for your help! 谢谢你的帮助!

document.write overwrites everything after the page has loaded (after the document is ready). document.write的页面加载后(文档准备好)覆盖一切。 The best solution would probably depend on the nature of the ad. 最佳解决方案可能取决于广告的性质。 If it is very simple (eg a single image), try document.createElement To make elements and then add them to parent elements with Parent.appendChild . 如果非常简单(例如单个图像),请尝试document.createElement制作元素,然后使用Parent.appendChild将它们添加到父元素中。

If the ad is more complex, do you have a given "parent node" where all your code can be added? 如果广告比较复杂,是否有一个给定的“父节点”可以在其中添加所有代码? If so, simply do parent.innerHTML = your_html_code . 如果是这样,只需执行parent.innerHTML = your_html_code

Also, iframes do not work that way. 另外,iframe不能那样工作。 They do not allow you to insert any random code by placing it inside the iframe tags. 它们不允许您将随机代码放置在iframe标记内来插入任何随机代码。 In fact, the content inside tags only show when the browser does not support the element . 实际上,标记内的内容仅在浏览器不支持element时显示 You have to have a source url from which to do it. 您必须具有执行此操作的源URL。 For reference, see MDN . 有关参考,请参见MDN

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

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