如何激活从iframe获取的javascript-tag?

时间:2022-12-05 22:22:32

I have a Rich Text Editor in Javascript in which I want to be able to enter an embedded widget in a Javascript tag. I.e:

我在Javascript中有一个富文本编辑器,我希望能够在Javascript标记中输入嵌入式窗口小部件。即:

<script src="http://www.mydomain.com/script.aspx?id=123" type="text/javascript"></script>

Still using JS I move the html-content from the iframe into a DIV in the page:

仍在使用JS我将iframe中的html内容移动到页面中的DIV:

<script>
    function copyRtfToDiv()
    {
        myiframe = document.getElementById('myiframe');
        postDiv = document.getElementById('blogPostDiv');
        content = myiframe.contentWindow.document.body.innerHTML;
        postDiv.innerHTML = content;
    }
</script>

When the content is copied into the DIV the script is not invoked. Is there a way to make this script run?

将内容复制到DIV时,不会调用脚本。有没有办法让这个脚本运行?

1 个解决方案

#1


First off, you seem to be copying JavaScript code into a DIV tags. That shouldn't really work, since it'll just treat it like regular text. To illustrate, compare these two blocks of code. The first, is a DIV:

首先,您似乎将JavaScript代码复制到DIV标记中。这应该不起作用,因为它只会像普通文本一样对待它。为了说明,比较这两个代码块。第一个是DIV:

<div>alert('hello!');</div>

And it'll look on the page, like this:

它会在页面上看起来像这样:

alert('hello!');

The second block of code, is a script:

第二个代码块是一个脚本:

<script>alert('hello!');</script>

Which will open a message box with the text "hello!", and await a user-click on "OK". But other than that, won't display anything in the page.

这将打开一个带有文本“hello!”的消息框,并等待用户单击“确定”。但除此之外,不会在页面中显示任何内容。

You're aiming for the 2nd option - one that actually runs the JavaScript code that resides between the tags.

您的目标是第二个选项 - 实际运行驻留在标记之间的JavaScript代码。

I'd recommend wrapping the script you want to invoke in a function, and invoking that function by name. So let's say you have within the IFRAME an HTML document whose body looks something like this:

我建议在函数中包装要调用的脚本,并按名称调用该函数。因此,假设您在IFRAME中有一个HTML文档,其正文如下所示:

<div id='jscode'>
   function mycode() {
       // This is some code!
       alert('Hello there!'); 
   }
</div>

You could then, in the IFRAME's parent document (the one containing the <iframe> tag), to use the following code in the parent to import it into the parent's DOM:

然后,您可以在IFRAME的父文档(包含

function copyRtfToDiv()
{
    myiframe = document.getElementById('myiframe');
    content = myiframe.contentWindow.document.GetElementById('jscode').innerHTML;
    newscripttag = document.createElement('script');
    newscripttag.innerHTML = content;
    mycode();
}

I have no clue why you would want to do something like this - which is basically re-parenting JavaScript code to live within you document's DOM; There are better ways to do this, not least of which is using a different editor that can be simply called externally. For example, tinyMCE and FCKEditor.

我不知道你为什么要做这样的事情 - 这基本上是重新编写JavaScript代码以生活在你文档的DOM中;有更好的方法可以做到这一点,尤其是使用可以简单地在外部调用的不同编辑器。例如,tinyMCE和FCKEditor。

Good luck!

#1


First off, you seem to be copying JavaScript code into a DIV tags. That shouldn't really work, since it'll just treat it like regular text. To illustrate, compare these two blocks of code. The first, is a DIV:

首先,您似乎将JavaScript代码复制到DIV标记中。这应该不起作用,因为它只会像普通文本一样对待它。为了说明,比较这两个代码块。第一个是DIV:

<div>alert('hello!');</div>

And it'll look on the page, like this:

它会在页面上看起来像这样:

alert('hello!');

The second block of code, is a script:

第二个代码块是一个脚本:

<script>alert('hello!');</script>

Which will open a message box with the text "hello!", and await a user-click on "OK". But other than that, won't display anything in the page.

这将打开一个带有文本“hello!”的消息框,并等待用户单击“确定”。但除此之外,不会在页面中显示任何内容。

You're aiming for the 2nd option - one that actually runs the JavaScript code that resides between the tags.

您的目标是第二个选项 - 实际运行驻留在标记之间的JavaScript代码。

I'd recommend wrapping the script you want to invoke in a function, and invoking that function by name. So let's say you have within the IFRAME an HTML document whose body looks something like this:

我建议在函数中包装要调用的脚本,并按名称调用该函数。因此,假设您在IFRAME中有一个HTML文档,其正文如下所示:

<div id='jscode'>
   function mycode() {
       // This is some code!
       alert('Hello there!'); 
   }
</div>

You could then, in the IFRAME's parent document (the one containing the <iframe> tag), to use the following code in the parent to import it into the parent's DOM:

然后,您可以在IFRAME的父文档(包含

function copyRtfToDiv()
{
    myiframe = document.getElementById('myiframe');
    content = myiframe.contentWindow.document.GetElementById('jscode').innerHTML;
    newscripttag = document.createElement('script');
    newscripttag.innerHTML = content;
    mycode();
}

I have no clue why you would want to do something like this - which is basically re-parenting JavaScript code to live within you document's DOM; There are better ways to do this, not least of which is using a different editor that can be simply called externally. For example, tinyMCE and FCKEditor.

我不知道你为什么要做这样的事情 - 这基本上是重新编写JavaScript代码以生活在你文档的DOM中;有更好的方法可以做到这一点,尤其是使用可以简单地在外部调用的不同编辑器。例如,tinyMCE和FCKEditor。

Good luck!