简体   繁体   English

在脚本标签内的其他HTML文件中运行JavaScript函数

[英]Run JavaScript Function in a different HTML file inside a script tag

Currently I have two HTML files. 目前,我有两个HTML文件。 One named index.html and another called editor.html 一个名为index.html,另一个名为editor.html

Inside index.html I have an iframe of editor.html Above this iframe is also a notification system I made. 在index.html中,我有一个editor.html iframe,在该iframe上方还创建了一个通知系统。 To run a notification it uses a function I created which can be used like so: 要运行通知,它使用我创建的函数,该函数可以这样使用:

Notification("msg");

This function works great when I call it from inside of the index.html since the function modifies the index.html's html code (via .innerHTML). 当我从index.html内部调用此函数时,该函数非常有用,因为该函数修改了index.html的html代码(通过.innerHTML)。 An issue shows up when I try and call it from the editor.html 当我尝试从editor.html调用时出现问题

Even with the editor.html added into index.html like this at the top of index.html: 即使将index.html顶部的editor.html添加到index.html中,也是如此:

<script src="editor.html"></script>

And I wrote this in the editor.html: 我是在editor.html中写的:

<script src="index.html"></script>

When I try and run the Notification function from the editor.html there is an error since the function is inside the index.html and modifies index.html, not editor.html. 当我尝试从editor.html运行Notification函数时,出现错误,因为该函数位于index.html内部,并且修改了index.html, 而不是 editor.html。

Keep in mind that in each of the index.html and editor.html the javascript is in a tag because there is other html within the file. 请记住,在每个index.html和editor.html中,javascript都在标记中,因为文件中还有其他html。 Thanks and please ask questions if you need further confirmation. 谢谢,如果您需要进一步确认,请提出问题。

This works fine if the iframe and the container are in the same domain. 如果iframe和容器在同一个域中,则此方法效果很好。

You could put the definition of Notification function in a single external file, like main.js: 您可以将Notification函数的定义放在单个外部文件中,例如main.js:

function Notification( msg )
{
    var div = document.getElementById("notification-div") ;
    div.innerHTML = msg ;
    /* The important thing is make this to be the container window when
     * Notification is called, so document is the desired one.
     */
}

In index.html you should have the iframe and a div to print the notification text: 在index.html中,您应该具有iframe和div来打印通知文本:

<div id="notification-div"></div>
<iframe src="editor.html" width=300 height=200></iframe>

Then include main.js in index.html: 然后在index.html中包含main.js:

<script type="text/javascript" src="main.js"></script>

In index.html you can call it directly, as long as this is window: 在index.html中,您可以直接调用它,只要它是window:

Notification( "From index.html" ) ;
/* window will be this in the Notification function, so the call to
 * document.getElementById, will actually be window.document.getElementById
 */

In editor.html you can refer to the container window object as top. 在editor.html中,您可以将容器窗口对象称为顶部。 So this call will give the desired result: 因此,此调用将产生所需的结果:

top.Notification( "From editor.html" ) ;
/* top will be this in the Notification function, so the call to
 * document.getElementById, will actually be top.document.getElementById
 */

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

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