最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - iframe .load event not firing - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 5

You 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

发布评论

评论列表(0)

  1. 暂无评论