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
.