Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iframe: How can we change iframe src from inside iframe itself?

I know there are question have been asked around resembling to this one, but still I am not getting proper concept of this. I am currently loading an iframe through javascript in following manner:

jQuery('.out-div-login').html("<iframe id='parent' frameBorder='0' width='320' height='500' src='"+_url+"'></iframe>");  

Which is working fine. (its cross domain). After loading my application inside given iframe, I want to come back to original state where .out-div-login was loading iframe into parent html.
From outside of iframe I can do this by accessing iframe using its id attribute, but not from the inside. Is there any way I can reload the iframe by giving its src again ? or by above code but from inside the iframe ? Thanks.
Update
I have tried below code without any success as of now:

var ifr = $('iframe[id="parent"]');  

from one of js file inside iframe with id parent. when I do console.log(ifr) it gives in firebug something like this Object[] instead of <iframe id="parent">

like image 746
user3137239 Avatar asked Dec 26 '13 16:12

user3137239


2 Answers

You should use location.href inside the iframe. That should work. set location.href in the iframe to your new location.

like image 121
Sebi2020 Avatar answered Oct 13 '22 13:10

Sebi2020


The url query string (?xxx) should also be kept in the new url, so the whole solution should be:

location.href = "new_link_url" + location.search;
like image 35
Yao Avatar answered Oct 13 '22 13:10

Yao