简体   繁体   English

如何将javascript文件插入iframe,然后在插入的javascript中调用函数?

[英]How to insert a javascript file into an iframe then call a function in the inserted javascript?

Edit: Just found out this is a chrome problem, the code works fine in firefox 编辑:刚发现这是一个chrome问题,代码在firefox中工作正常

I have an iframe on a webpage that shows a book formatted as html. 我在网页上有一个iframe,显示格式为html的图书。 I would like to insert some javascript within this iframe to make the book more dynamic (eg click on sentences, show animations etc). 我想在这个iframe中插入一些javascript来使这本书更具动态性(例如点击句子,显示动画等)。 The iframe content is in the same domain as the parent page. iframe内容与父页面位于同一域中。

I can insert the javascript into the iframe but get an error calling a function in the inserted javascript. 我可以将javascript插入iframe,但在插入的javascript中调用函数时出错。 I've described the different bits of code below: 我已经描述了下面的不同代码:

My parent page javascript is: 我的父页面javascript是:

function iframeLoaded()
{
    var iFrameID = document.getElementById('preview-iframe');

    var jsLink = iFrameID.contentDocument.createElement("script");
    jsLink.src="/tests/iframeAPI.js";
    jsLink.type = 'text/javascript';
    iFrameID.contentDocument.head.appendChild(jsLink); 
    iFrameID.contentWindow.initialiseApi()
}

and the html containing the iframe is: 并且包含iframe的html是:

<iframe id="preview-iframe" width="640" height="240" frameborder="0" src="./testpage.htm" onload="iframeLoaded()" scrolling="no"></iframe>

The contents of iframeAPI.js is: iframeAPI.js的内容是:

window.initialiseApi = function() { alert("Hello world") }

Looking at the iFrame's html in the browser shows that the iFrameAPI.js tag is inserted ok into the iframe head, but I don't get the alert popup when the page is loaded. 在浏览器中查看iFrame的html显示iFrameAPI.js标记已插入到iframe头中,但是在加载页面时我没有收到警报弹出窗口。 The error appears on the following line: 错误显示在以下行:

    iFrameID.contentWindow.initialiseApi()
    Uncaught TypeError: Object [object Window] has no method 'initialiseApi' 

However I can run this line in the browser's javascript console and the alert popup works fine. 但是我可以在浏览器的javascript控制台中运行此行,并且警报弹出窗口正常工作。

Any help would be much appreciated. 任何帮助将非常感激。

Thanks, 谢谢,

Brian 布赖恩


Edit: I've just tried with an onload event to make sure the page is loaded and I still have the problem: 编辑:我刚尝试使用onload事件来确保页面已加载,我仍然遇到问题:

My parent page javascript is now : 我的父页面javascript现在是:

function iframeLoaded()
{
    var iFrameID = document.getElementById('preview-iframe');

    var jsLink = iFrameID.contentDocument.createElement("script");
    jsLink.src="/tests/iframeAPI.js";
    jsLink.type = 'text/javascript';
    iFrameID.contentDocument.head.appendChild(jsLink); 
    jsLink.onLoad= iFrameLoaded();
}

function iFrameLoaded()
{
    alert("Iframe loaded"); // Alert works ok 
    var iFrameID = document.getElementById('preview-iframe');
    iFrameID.contentWindow.initialiseApi(); // Same error message on this line
}

It sounds like you are trying to use the function before the content has loaded. 听起来你在内容加载之前尝试使用该功能。

try this instead: 试试这个:

var t = setTimeout(iFrameID.contentWindow.initialiseApi(),500);

This will wait half a second before trying the function which should give the page tiem to load. 这将等待半秒,然后再尝试加载页面tiem的函数。 Delay times are given in milliseconds. 延迟时间以毫秒为单位。

An even better approach is to try using Jquery and its ready() method but this requires the jquery library to be loaded as well. 更好的方法是尝试使用Jquery及其ready()方法,但这也需要加载jquery库。 Its well worth it though in my opinion, see http://api.jquery.com/ready/ . 它在我看来非常值得,请参阅http://api.jquery.com/ready/

You would try something like: 你会尝试类似的东西:

$("body",iFrameID.contentWindow.document).ready(iFrameID.contentWindow.initialiseApi())

You're executing it right away without giving the script a chance to load. 你正在执行它而不给脚本一个加载的机会。 Hook up an onload event to your script block and run your main function then. 将onload事件连接到脚本块并运行主要功能。

Try, in the page included in the iFrame, accessing the main page by doing something like: 尝试在iFrame中包含的页面中,通过执行以下操作来访问主页:

window.parent.xyz = something;

Where something is what you want exposed to the main page. 哪些something是你想要暴露在主页面的东西。 Could be a function or an object of functions. 可以是函数的函数或对象。 Now in the main page you can just do: 现在在主页面中你可以做到:

something(); // or something.somefunction();

You could also send window references, I think, but I have not tried that. 我想你也可以发送窗口参考,但我还没试过。

The easiest way is to call the initialiseApi function in the iframeAPI.js itself as it will be called as soon as it's loaded. 最简单的方法是在iframeAPI.js本身中调用initialiseApi函数,因为它将在加载后立即调用。 The iframeAPI.js could look like that: iframeAPI.js看起来像这样:

function initialiseApi() { 
    alert("Hello world"); 
}
initialiseApi();

There is no callback or timeout needed. 不需要回调或超时。

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

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