简体   繁体   English

外部js文件中的javascript document.write

[英]javascript document.write in external js file

Working on big, high loaded project I got the problem that already described in billion of topics on forums and blog, but there is no solution that will help in my case. 在大型高负载项目上工作我遇到了问题,已经在论坛和博客上描述了数十亿个主题,但是没有解决方案可以帮助我。 Here is the story. 这是故事。

I have the HTML code of banner, I don't know what is the code. 我有横幅的HTML代码,我不知道代码是什么。 Sometimes it's plain HTML, but sometimes it's <script> tag with document.write inside it with <script> tag that has src to doubleclick network in it. 有时它是纯HTML,但有时它是带有document.write的<script>标签,其中<script>标签有src来双击网络。

So we have: script > document.write > script(doubleclick). 所以我们有:script> document.write> script(doubleclick)。

doubleclick network, as you may know, use document.write too and most of the time they give flash banners that need to load one more js file. 正如您所知,doubleclick网络也使用document.write,并且大部分时间他们都提供需要加载一个js文件的flash横幅。

So after all we have: script > document.write > script(doubleclick) > document.write > script > ... 毕竟我们有:script> document.write> script(doubleclick)> document.write> script> ...

This works good when you place it in HTML directly. 当您直接将它放在HTML中时,这很有用。 Page rendering 1 part, load banner1, keep rendering page, load banner2, finalizing page rendering. 页面渲染1部分,加载banner1,保持渲染页面,加载banner2,完成页面渲染。

But right now I need to render page first and only after that load banners. 但是现在我需要首先渲染页面,并且仅在该负载横幅之后。 As banner use document.write I need to load it before window.onload event (note: after window.onload document.write will rewrite whole document.) 作为banner使用document.write我需要在window.onload事件之前加载它(注意:在window.onload document.write之后将重写整个文档。)

What I've done: 我做了什么:

In the head section I have an banners object(real namespace kind of huge :)), with property scope. 在头部我有一个横幅对象(真正的名称空间类型巨大的:)),具有属性范围。

When page rendering and banner code is meet I place the code of the banner into the scope and put <div id="bannerPlaceHolder"+id></div> -- so here I will need to put banner content later on 当页面呈现和横幅代码相遇时,我将横幅的代码放入范围并放入<div id="bannerPlaceHolder"+id></div> - 所以这里我需要稍后放置横幅内容

Page rendered and before </body> tag I put <script>banners.load()</script> banners.load method do this for each item in scope array: 页面呈现和</body>标记之前我把<script>banners.load()</script> banners.load方法为范围数组中的每个项执行此操作:

document.write('<div id="codeHolder'+id+'">');
document.write(bannerCode);
document.write('</div>');

And only after this I have window.onload() event that do this: 只有在此之后,我才能执行以下操作的window.onload()事件:

take all banners codeHolders and node-by-node append it nodes from codeHolder to placeHolder, so in result I have loaded banners after rendering the page and banners are on the right places. 采取所有横幅codeHolders和逐个节点将其从codeHolder的节点附加到placeHolder,因此结果我在渲染页面后加载了横幅,横幅位于正确的位置。

All is perfect except IE, it load any js script that was putted in DOM dynamically in asynchron way, so document.write inside doubleclick scripts append nodes to the end of the document and not in my codeHolder nodes. 除了IE之外,它都是完美的,它以异步方式动态加载任何在DOM中放置的js脚本,因此doubleclick脚本中的document.write将节点附加到文档的末尾而不是我的codeHolder节点。 As usual it's only in IE. 像往常一样,它只在IE中。

I will be really appreciated to anyone who may know the solution. 任何可能知道解决方案的人都会非常感激。

You need writeCapture.js (full disclosure: I'm the author.) All bets are off with 3rd party scripts. 你需要writeCapture.js (完全披露:我是作者。)所有的赌注都与第三方脚本有关。 Today they use document.write to generate some specific HTML, but tomorrow they could change it and any simple hacks based on replacing document.write will need to be updated. 今天他们使用document.write生成一些特定的HTML,但是明天他们可以更改它,并且需要更新任何基于替换document.write简单hacks。 writeCapture takes care of that. writeCapture会处理这个问题。 Your usage would be something like: 您的用法如下:

$('#bannerPlaceHolder').writeCapture().html('<script type="text/javascript" src="http://doubleclick.net/bannerCode.js"></script>');

The library can handle any arbitrary depth of script tags and does just fine with interspersed HTML. 该库可以处理任何深度的脚本标记,并且可以很好地处理散布的HTML。 It also has a standalone build if you don't use jQuery. 如果你不使用jQuery,它也有一个独立的构建。

通常使用横幅,您就会知道确切的尺寸和位置,那么如何使用bannerCode中的内容动态创建iframe,这就是全部。

Split the document.write()-calls in separate -tags, so the output should look like: 拆分document.write() - 在单独的-tags中调用,因此输出应如下所示:

document.write(''); 文件撰写( ''); document.write(bannerCode); 文件撰写(bannerCode);

document.write(''); 文件撰写( '');

The IE will wait for document.write(bannerCode) is ready and when writes the closing -tag. IE将等待document.write(bannerCode)准备就绪,并在写入结束标签时。

perhaps you could use the property innerHTML: document.getElementById("x").innerHTML="................."; 也许你可以使用属性innerHTML:document.getElementById(“x”)。innerHTML =“.................”;

Or you could you the DOM: CreateElement and AppendChild 或者你可以使用DOM:CreateElement和AppendChild

将其加载到隐藏(大小为0x0)的iframe中,然后使用javascript将其移动到其他位置

I think iframe solution is ugly. 我认为iframe解决方案很难看。 Why cant you load the banners into a separate page and AJAX them into a DIV? 为什么你不能将横幅加载到一个单独的页面并将它们AJAX加载到DIV中?

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

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