0

I am calling a javascript from within a iframe to redirect to another url.

var new_url  = "http://mysite.com/?language=en&origin="
                    + origin +"&destination="+ destination +                    "&Date=" + date;
    }
    //window.location.replace(new_url);
    window.top.location.href = new_url;
    //window.location.assign(new_url);

purpose is to open the page out of iframe within same window so i am using

window.top.location.href = new_url;

It is working finr in IE but not in chrome and FF though not giving any error also

Aquarius24
  • 1,806
  • 6
  • 33
  • 61
  • Are you sure `window.location` wouldn't work in this case (i don't use iframes much, but the window variable should still be the same, correct?)? Also `top.window` should select the topmost window. – UIlrvnd Sep 10 '13 at 05:24
  • http://stackoverflow.com/questions/13209214/document-location-href-not-working-in-chrome – Parfait Sep 10 '13 at 05:26
  • If I got your question correctly, `window.location.href = new_url` should work – Jashwant Sep 10 '13 at 05:26

4 Answers4

3

Latest browsers have strict rules and do not allow to change, only if:

  1. The locations of A and B have the same origin, which is to say they have the same scheme, host, and port (HTTP, stackoverflow.com, 80 for example), or
  2. B is a top-level window, and A is a window in a frame nested at some depth within B (direct child, child of a child, etc.), or
  3. B is a window opened using window.open and A can change the location of the window that opened B (so B is a popup opened by A, by a popup window opened by A, or at greater depth), or
  4. B isn't a top-level window, but its parent window, or its parent's parent window, or at some similar amount of parentage the locations of that window and A are same-origin
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
1

Try only this, top is not required.

window.location.href = new_url;
Amit
  • 15,217
  • 8
  • 46
  • 68
1

Yes you can use window.location.href for your case.

But I thought of explaining the difference between the two.

window.location.href returns the location of the current page.

top.location.href (which is an alias of window.top.location.href) returns the location of the topmost window in the window hierarchy. If a window has no parent, top is a reference to itself (in other words, window === window.top).

top is useful both when you're dealing with frames and when dealing with windows which have been opened by other pages. For example, if you have a page called test.html with the following script:

var newWin=window.open('about:blank','test','width=100,height=100');
newWin.document.write('<script>alert(top.location.href);</script>');

The resulting alert will have the full path to test.html – not about:blank, which is what window.location.href would return.

Cheers!!!

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
Amit
  • 2,495
  • 14
  • 22
1

I have just added

window.event.returnValue = false;

before redirecting and it's working in all browsers

Aquarius24
  • 1,806
  • 6
  • 33
  • 61