13

I have a in 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 to have the iframe call a function from the parent page, that will cause the iframe to change url? I also had a basic qustion, if I have an iframe, and click on a link that brings me to a new page, does 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

Christian
  • 27,509
  • 17
  • 111
  • 155
Dave Smith
  • 177
  • 1
  • 3
  • 11

3 Answers3

20

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

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

parent.sendMeToGoogle();

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

function sendMeToGoogle(){
    document.getElementById('iframeID').src="http://google.com/";
}
Dani
  • 3,128
  • 2
  • 43
  • 91
Christian
  • 27,509
  • 17
  • 111
  • 155
  • Hi Christian,Thanks for your reply. I understand everything except ('theiframe') what value goes their? – Dave Smith Aug 13 '11 at 01:13
  • Hi Christian, thanks for your help. It still doesn't seem to work for me. Here's what i have for the parent page: – Dave Smith Aug 14 '11 at 07:30
  • And here's what I have for the child page:
    – Dave Smith Aug 14 '11 at 07:34
  • 1
    Try `top` instead of `parent`. – Christian Aug 14 '11 at 23:11
8

If what you really need is 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.

iamnicoj
  • 487
  • 7
  • 13
0

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

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

Steven
  • 2,437
  • 5
  • 32
  • 36
  • Hi Steven, thanks for your reply. Ultimately what Ii am trying to do is have a parent page with an iframe. The ifrmae will link to different pages that all link back to the initial iframe url. Each time the initial ifram url loads into the iframe, I want it to add one value to a variable that is in the oarent page, when that variable reaches ten, I want the either the iframe, or the parent page to redirect to a new url – Dave Smith Aug 14 '11 at 07:37
  • Hi Dave. You can indeed do this the way you are trying. Everytime the initial page loads, you do a javascript call to the parent. The value on the parent page will not be erased when you change pages in the iframe. – Steven Aug 14 '11 at 12:46