[英]iframe .load event not firing
我正在嘗試重新加載 iframe,如下所示:
var frameObject = $('#myFrame');
var frameAsInDom = frameObject[0].contentWindow;
var currenctLocation = frameAsInDom.location;
frameObject.attr('src', 'about:blank');
frameObject.contents().remove(); // This was a desperate statement!!
frameObject.attr('src', currentLocation).load(function(){
alert('iframe reloaded');
});
問題是 iframe 的 .load 事件根本沒有被觸發。 我沒有在負載回調中收到警報。
您正在混合事件的綁定和觸發,
請參閱此代碼: http : //jsfiddle.net/BvQuk/1/
HTML
<iframe id="myFrame" src="http://google.com"></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
});
});
希望有幫助。
您有一個 JavaScript 錯誤: currentLocation
未定義。 這是由於var currenctLocation
上的拼寫錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.