动态插入的JavaScript到HTML中使用的document.write [英] Dynamically inserting javascript into HTML that uses document.write

查看:349
本文介绍了动态插入的JavaScript到HTML中使用的document.write的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前加载收藏夹式的弹出窗口,加载它的HTML从XHR调用。此内容,然后利用显示在模式弹出 element.innerHTML =含量这就像一个魅力。

I am currently loading a lightbox style popup that loads it's HTML from an XHR call. This content is then displayed in a 'modal' popup using element.innerHTML = content This works like a charm.

在本网站的另一部分我用的是Flickr的徽章(<一href="http://www.elliotswan.com/2006/08/06/custom-flickr-badge-api-documentation/">http://www.elliotswan.com/2006/08/06/custom-flickr-badge-api-documentation/)动态加载Flickr的图像。这样做是包括加载的Flickr的JavaScript,这反过来做一些文件撰写 statments的脚本标记。

In another section of this website I use a Flickr 'badge' (http://www.elliotswan.com/2006/08/06/custom-flickr-badge-api-documentation/) to load flickr images dynamically. This is done including a script tag that loads a flickr javascript, which in turn does some document.write statments.

包含在HTML的时候他们都很好地工作。只有加载的Flickr徽章code的里面的时候的灯箱,没有内容呈现在所有。看来,使用的innerHTML 文件撰写语句是把它一步太远,但我找不到任何线索在JavaScript实现(FF2和3,IE6和7)的这种行为

Both of them work perfectly when included in the HTML. Only when loading the flickr badge code inside the lightbox, no content is rendered at all. It seems that using innerHTML to write document.write statements is taking it a step too far, but I cannot find any clue in the javascript implementations (FF2&3, IE6&7) of this behavior.

任何人都可以澄清这是否应该或不应该工作?谢谢你。

Can anyone clarify if this should or shouldn't work? Thanks.

推荐答案

在一般情况下,使用的innerHTML时,脚本标记不执行。在你的情况,这是件好事,因为文件撰写通话将消灭一切的已经在页面中。但是,让你没有任何HTML文件撰写应该增加。

In general, script tags aren't executed when using innerHTML. In your case, this is good, because the document.write call would wipe out everything that's already in the page. However, that leaves you without whatever HTML document.write was supposed to add.

jQuery的HTML的操作方法将在HTML中执行脚本,诀窍,然后捕捉调用文件撰写并获得在正确的位置的HTML。如果它足够简单,那么这样的事情会做的:

jQuery's HTML manipulation methods will execute scripts in HTML for you, the trick is then capturing the calls to document.write and getting the HTML in the proper place. If it's simple enough, then something like this will do:

var content = '';
document.write = function(s) {
    content += s;
};
// execute the script
$('#foo').html(markupWithScriptInIt);
$('#foo .whereverTheDocumentWriteContentGoes').html(content);

它得到尽管复杂。如果脚本是在另一个领域,这将异步加载,所以你必须要等到它完成获取内容。此外,如果它只是写HTML到片段中间没有一个包装元素,你可以很容易地选择? writeCapture.js (全面披露:我写的)处理所有这些问题。我建议你​​只是用它,但最起码​​,你可以看看code,看看它是如何处理的一切。

It gets complicated though. If the script is on another domain, it will be loaded asynchronously, so you'll have to wait until it's done to get the content. Also, what if it just writes the HTML into the middle of the fragment without a wrapper element that you can easily select? writeCapture.js (full disclosure: I wrote it) handles all of these problems. I'd recommend just using it, but at the very least you can look at the code to see how it handles everything.

编辑:这是一个页面证明什么听起来像你想要的效果

Here is a page demonstrating what sounds like the effect you want.

这篇关于动态插入的JavaScript到HTML中使用的document.write的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆