Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which one is better approach window.parent.location.href or window.top.location

I am working in a project where I have to redirect on Error Page in a particular scenario. For that I have created Error.aspx page. Right now I am using window.top.location.href = "../Error.aspx" and it generate http://localhost/app_web/Error.aspx and its working fine except once (which shows Message http://xyz/ErrorPage.aspx' does not exist. ). So can anyone suggest which is the better option for this.

Thanks

like image 718
Haseeb Akhtar Avatar asked Mar 27 '12 12:03

Haseeb Akhtar


2 Answers

top is "better than" parent if your intent is to framebust your page into the top level, because your page may be inside a frame that is itself inside a frame.

As for your relative path problem, you may want to try:

var local = location.pathname.split("/");
local.pop(); // remove the filename
local.pop(); // remove the containing directory
local.push("Error.aspx");
local = location.protocol+"//"+location.hostname+"/"+local.join("/");
top.location.href = local;
like image 134
Niet the Dark Absol Avatar answered Sep 16 '22 16:09

Niet the Dark Absol


window.parent refers to the parent window of the current window. That parent may have it's own parent, which has its own parent etc.

window.top refers to this top most window; e.g. window.parent.parent.parent[...];

In this circumstance however, you probably only want to redirect the current window, e.g;

window.location.href = "../Error.aspx";

For more info, see the documentation on window.parent, window.top and window.location.

like image 28
Matt Avatar answered Sep 18 '22 16:09

Matt