Forum Moderators: open

Message Too Old, No Replies

document.write(opener.location.href)

         

Taoula

11:33 am on Jun 5, 2004 (gmt 0)



I'm weak at coding JS and have a question about reading the location (URL) of a parent window from a child window - I have no problems writing it to change the location of a parent window but have a need to check and verify the URL of the parent window.

I've tried a lot of different schemas I've found on the internet but keep running into an 'Access Denied' or 'Permission Denied' javascript error in the browser.

Basically, I get a lot of traffic from sites that open their link to my site in a new window, so there is no referrer information in the HTTP headers. My goal is to capture the URL in the parent window to identify where the visitor came from if the referrer is missing - does that make sense?

More importantly, is there a way to do this that I'm missing?

Thanks loads!
Anastasia

BlobFisk

12:13 pm on Jun 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, Taoula!

You need to make sure that you are opening the child window in an event, rather than as part of the href. For example:


<a href="tmp2.html" onclick="window.open('tmp2.html'); return false" title="Open Window">OPEN ME</a>

Then in your child window you can access the parent url like this:


<script type="text/javascript">document.write(window.opener.location.href);</script>

You can then do you checking by using an if conditional statement.

HTH

DrDoc

2:55 pm on Jun 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



capture the URL in the parent window to identify where the visitor came from if the referrer is missing

That is not possible... You will not have access to that information unless the other site is on the same domain as yours. Furthermore, as BlobFisk said, window.opener doesn't work on regular links...

The referrer is your safest bet (though it cannot be trusted blindly).

Rambo Tribble

3:17 pm on Jun 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The "Access Denied" and DrDoc's comment relate to the same thing: the "Same Origin Policy" which prevents scripts from accessing pages outside their own domain.

The problems you have with document.referrer may relate to known bugs in IE. A server-side script may serve you better.