开发者

Change URL in an iframe using javascript

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


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/";
}


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.


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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜