views:

32

answers:

2

I am trying to reload an iframe like the following:

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');
});

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.

+1  A: 

duh!! javascript error.. currentLocation is undefined. typo on var currenctLocation

working demo

Reigel
sorry for the typo... Have made an update in the fiddle.. the load call back is not firing properly..http://jsfiddle.net/4tRWq/2/
ZX12R
[demo](http://jsfiddle.net/4tRWq/3/) <-- look at the demo. and try to realize it based on the alerts made.
Reigel
your problem is your binding `.load()` twice...
Reigel
i guessed it.. but even if i unbind before binding again it doesn't work http://jsfiddle.net/4tRWq/4/
ZX12R
but what do you think with [this one](http://jsfiddle.net/4tRWq/5/)?
Reigel
i don't understand the difference.. can u explain?
ZX12R
In my latest update, I bind `load-event` first, then change the source, was there still a unexpected behavior?
Reigel
i noticed the change in the order.. i couldn't understand the difference the order event addition is going to make..yes..still the "reload" alert didn't fire..
ZX12R
and how is this working http://jsfiddle.net/4tRWq/6/ i am totally confused
ZX12R
You are mixing the binding and triggering of the event, see my simplified answer.
xmarcos
+1  A: 

You are mixing the binding and triggering of the event,

See this code: http://jsfiddle.net/BvQuk/1/

HTML

<iframe id="myFrame" src="http://google.com"&gt;&lt;/iframe&gt;&lt;br/&gt;
<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.

xmarcos
what if i need different handlers for loading and reloading?
ZX12R
Loading and re-loading are (internally) the same, you request a web page to be loaded inside an iframe, what you can do is listen for the first load and fire an event there as well, if that is the case, let me know and I'll give you an example. Regards.
xmarcos
the problem is this...initially when i assign a src to the iframe a load event is attached... when i reassign the url to the iframe i attach a different load event handler... after reassigning the first handler fires and not the second..
ZX12R
You don't seem to understand how the event model works. The example is working as it should and the events are firing when and how they are supposed to.If you want to perform a different action on first load, you can do so like this http://jsfiddle.net/BvQuk/2/.
xmarcos