问题
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
回答1:
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
.
来源:https://stackoverflow.com/questions/1163544/javascript-window-parent-issue