Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1807 How do I get a child window to close after clicking on a link that opens in the parent window?

You are here: irt.org | FAQ | JavaScript | Window | Q1807 [ previous next ]

A spawned child window contains a link that is supposed to open in the parent, then the child is supposed to close. This is similar to the Javascript:Windows FAQ 84, yet decidedly different.

A simple onclick="window.close" won't work in this case because the window will close before it has a chance to process the link. So, use the code for FAQ 84 and attach a window.close to the end. This forces the child to think about what he's done and process the link before closing.

<head>:
<script language="JavaScript"><!--
function load(file,target) {
  if (target != '') {
    target.window.location.href = file;
  } else {
    window.location.href = file;
  }
  window.close();
}
//--></script>

The link:
<a href="javascript:load('thelink.html',opener)">

Submitted by Dan

©2018 Martin Webb