Changing url in iframe using javascript

I have an iframe that calls a function from the parent page. The function is window.location, however this does not change the URL. Is there a way for an iframe to call a function from the parent page, which will cause the iframe url to change? I also had a basic qustion, if I have an iframe, and click the link that will lead me to the new page, will the parent page remain open?

Thanks in advance for your help. Sorry if I sound like a complete idiot, I am new to javascript.

Dave

+7
source share
3 answers

window.location not a function, it is an object .

To do what you want, first make the iframe a special function call from it.

 parent.sendMeToGoogle(); 

And in the function (in the parent) do something like:

 function sendMeToGoogle(){ document.getElementById('iframeID').src="http://google.com/"; } 
+10
source

If you really need to change the parent URL, you can use window.top.location.href='http://anotherURL.com' , even if they are in different domains, from the iframe page.

+3
source

I assume you want to do more in the function of the parent page; if not, you can just change the iframe url without calling the parent, of course ...

As for your second question, the iframe behaves like an ebmedded page: you can view everything you want in the iframe without affecting the parent (except, of course, javascript calls like the one you want to use), but view with the parent page and you will also lose the iframe. Hope this was the explanation you were looking for :)

0
source

All Articles