Calling a page via a link with a target attribute

Filed under: JavaScript

comments (0) Views: 5,964

Today over at House of Fusion Claude Schneegans asked:

Suppose I have a page like myPage.cfm. Is it possible in this page to detect if it was called from a link having a TARGET="..." attribute?

I decided to throw something together to determine if this was indeed possible. Turns out it is, and here's how I accomplished it. I first whipped up a plain HTML page with two links on it, like so:

<a href="child.html" target="_blank">Open child.html with target="_blank"</a>
<br /><br />
<a href="child.html">Open child.html as normal window</a>

I then wrote a companion page which used JavaScript to try and determine the "parent"

var opener = (parent.opener) ? parent.opener.location.href : 'an unknown window';

Finally, I used jQuery to write the result of the test to a span tag on the child page. Admittedly this has limited use, but it's a nice means of determining if a target attribute was used to open the window, which essentially gives you access to the parent page with JavaScript if that page is still open.

View the demo.

Amazon logo

If this article was interesting, or helpful, or even wrong, please consider leaving a comment, or buying something from my wishlist. It's appreciated!

comments powered by Disqus