Home  >  Article  >  Web Front-end  >  js iframe的onload事件不去作用的原因及解决办法

js iframe的onload事件不去作用的原因及解决办法

WBOY
WBOYOriginal
2016-06-01 09:54:452562browse

通过createElement_x_x创建的iframe的onload事件在有些时候不响应,具体原因和解决办法如下。

首先,我们来看一下下面这种方法:

<code class="language-javascript">function createFrame(){ 
    var frame = document.cerateElement("iframe");
    frame.onload = function(){alert("loaded - createFrame");}
    frame.src = "http://www.phper.org.cn";
    document.body.a(frame);

}</code>

这一段在FF下是完全如我们所想,能够达到预期效果,但是在IE下面似乎就失效了,导致这种结果的原因还不是十分清楚,有一种说法是IE在检测到 iframe的时候,就把认为它已经装载完成了,通过createElement_x_x创建的iframe,似乎在创建的一瞬间它的onload就完成了,后面的onload设定大概也就无效了。

下面我们再来看一个方法:

<code class="language-javascript">function includeFrame(){
   var div = document.create_rElement_x("div");
   div.innerHTML = "<iframe onload="\"javascript:alert('loaded" src="%5C%22http://www.manongjc.com%5C%22"></iframe>";

   document.body.a(div);
}</code>

这个方法在IE和FF下面测试都通过,上面的那种说法虽然不是很有根据,但是目前还没有找到更好的解释。如果真的是如上面所说的话,那通过第二种方法正好可以解决,因为在第二种方法中iframe的onload事件跳过createElement_x_x("iframe")这个步骤,避免了IE的检测。

onload必须在src前面才可以.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn