admin管理员组文章数量:1326478
I am trying to reload an iframe like the following:
var frameObject = $('#myFrame');
var frameAsInDom = frameObject[0].contentWindow;
var currentLocation = frameAsInDom.location;
frameObject.attr('src', 'about:blank');
frameObject.contents().remove(); // This was a desperate statement!!
frameObject.attr('src', currentLocation).load(function(){
alert('iframe reloaded');
});
The problem is the iframe's .load event is not getting triggered at all. I am not getting the alert inside the load call back.
I am trying to reload an iframe like the following:
var frameObject = $('#myFrame');
var frameAsInDom = frameObject[0].contentWindow;
var currentLocation = frameAsInDom.location;
frameObject.attr('src', 'about:blank');
frameObject.contents().remove(); // This was a desperate statement!!
frameObject.attr('src', currentLocation).load(function(){
alert('iframe reloaded');
});
The problem is the iframe's .load event is not getting triggered at all. I am not getting the alert inside the load call back.
Share Improve this question edited Dec 9, 2022 at 10:40 Jake 1,0779 silver badges20 bronze badges asked Sep 28, 2010 at 6:50 ZX12RZX12R 4,8269 gold badges41 silver badges54 bronze badges2 Answers
Reset to default 5You are mixing the binding and triggering of the event,
See this code: http://jsfiddle/BvQuk/1/
HTML
<iframe id="myFrame" src="http://google."></iframe><br/>
<input id="uxButton" type="button" value="Reload the frame" />
JS
$(document).ready(function() {
var iFrame = $('#myFrame');
iFrame.bind('load', function() { //binds the event
alert('iFrame Reloaded');
});
$('#uxButton').click(function() {
alert('onButtonClick OR onBeforeReload');
var newSrc = iFrame.attr('src') + '?c=' + Math.random(); //force new URL
iFrame.attr('src', newSrc); //changing the src triggers the load event
});
});
Hope that helps.
You have a JavaScript error: currentLocation
is undefined. It's due to a typo on var currenctLocation
.
working demo
本文标签: javascriptiframe load event not firingStack Overflow
版权声明:本文标题:javascript - iframe .load event not firing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742206648a2432940.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论