views:

1088

answers:

1

Hello everyone,

I am new to Javascript and after reading related books for quite some time, I am still confused what is the meaning and what is the function of window.parent? Appreciate if someone could show me some simple samples to let me know what does window.parent mean? Thanks!

Here is the code I am confused, and it is a part of Javascript code written by a ASP.Net class as a part of response to client side. I am especiallt confused about what means window.parent." + Taget + ".location = '" + url. Appreciate if anyone could make it clear.

HttpContext.Current.Response.Write("<script>window.parent." + Taget + ".location = '" + url + "?userID=" + userID + "';window.location='Title.aspx';</script>");

thanks in advance, George

+6  A: 

window.parent refers to a frame's (or iframe's) parent:

<frameset cols="25%,75%">
   <frame src="frame_a.aspx" name="frameA" />
   <frame src="frame_b.aspx" name="frameB" />
</frameset>

In the above example, if window.parent were executed in frame_a.aspx, it would refer to the window containing the <frameset> element.

Target refers to either a frame (by name) or a standard target:

  • _blank - New window
  • _parent - Current frame's parent
  • _top - Top-most frame (entire browser window/tab)

_top and _parent only refer to different things if your frames are more than one level deep (eg. if frame_a.htm contained another frameset or iframe)

'window.parent.' + target + '.location' is changing the URL of a frame, contained within the current frame's parent, with the name represented by the variable target. (I have assumed taget is simply a typo).

In my above example, if frame_a.aspx executed your example code with the target variable "frameB", it would change the url of that frame to something else (without affecting frameA).

Although you haven't mentioned it, it's possible you are using window.open and are trying to change the location on the window that opened it. In that case, you are looking for window.opener.

Richard Szalay
Thanks Richard, 1. I want to confirm with you that in my sample, I will change the current frame's new URL to be Title.aspx, and change the new URL of the frame (named by variable Target) to the value assigned by variable url, correct? 2. for the target you mentioned, it is just a normal customer defined variable, not javascript reserved variable?
George2
1. That is correct (though I hadn't scrolled to the right to see the Title.aspx bit ;) 2. target is not a reserved word, it's just a variable (a C#/VB.NET variable in your case, since you are generating javascript server side)
Richard Szalay
Thanks Richard, question answered. I have a related javsscript question here, appreciate if you could help. :-)http://stackoverflow.com/questions/1163660/http-redirection-issue
George2