March 27, 2012

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

Question by Haseeb Akhtar

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

Answer by Kolink

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;

Answer by Starx

It depends on what you are trying to do.

  1. window.parent.location

    is used to change the location of the parent window.

  2. window.top.location

    • It is a property of the object ‘window’.
    • It returns the location of the topmost window in the window hierarchy.
    • If a window has no parent, top is a reference to itself (window === window.top)

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!