简体   繁体   中英

JavaScript window.parent issue

I am new to JavaScript and after reading related books for quite some time, I am still confused about 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 an ASP.NET class as a part of the response to client-side. I am especially 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

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' '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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM