2

I need to implement parent page redirection from iframe. I know that it is impossible to do in different domains due to browsers security. However I found that links have target attribute and tried to use it in the following way:

<a href="http://google.com" target="_top" id="testParentRedirect">someLink</a>

It works fine if I click this link manually, but I couldn't find cross-browser solution to simulate it using JavaScript.

document.getElementById('testParentRedirect').click();

This works fine in IE, however Firefox and Safari don't know click function :).

I tried to work with jQuery, but for some reason they don't simulate click event for links. (see following post)

I couldn't find any appropriate solution on Stack Overflow. Maybe someone could help me in it. I will appreciate it. :)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Danil
  • 1,883
  • 1
  • 21
  • 22
  • 1
    possible duplicate of [Is it possible to trigger a link's (or any element's) click event through JavaScript?](http://stackoverflow.com/questions/143747/is-it-possible-to-trigger-a-links-or-any-elements-click-event-through-javascr) – roryf Jun 07 '10 at 14:45
  • Oops, guys, sorry. I was wrong about setting location of the parent. It is possible to set it but it is impossible to get due to browsers security. – Danil Jun 07 '10 at 15:12

2 Answers2

7

You can do this in javascript to exit a frame:

window.top.location = "http://google.com";
Keltex
  • 26,220
  • 11
  • 79
  • 111
3

You can try

top.location.replace( "http://google.com" );

in javascript to "escape" from the frame.

Edit: Using replace is slightly nicer, changed my answer to use that.

Mat
  • 6,694
  • 7
  • 35
  • 39