Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem getting a javascript referrer from a window.opener

Status
Not open for further replies.

mikebutler

Programmer
Oct 15, 2002
6
GB
I have some code which I use to distinguish the referrer to a framed site. Due to the fact that I want all frames and the frameset to return the same value I use parent.document.referrer to get the referrer. Unfortunately this sometimes return the value of the site.
lets call the referrer and my site :). Sometimes the referrer is but occaisionally it is NULL or I have discovered that when you open in a new window from you get NULL... so I used an if statement to look at this... e.g. window.opener.document.referrer and this resulted in better results but also from some referrers I get a JS error pop up saying that window.opener.document.referrer is null or not a valid object. I'm now spiralling... mainly downwards ;-) can anyone give me some pointers as to what code will guarentee the correct referrer regardless of how is opened. Many thanks too anyone who can end the misery :-|
 
Maybe you could detect whether or not window.opener exists.
if(window.opener){
r=window.opener.document.referrer;
}
else{
r=document.referrer;
}
 
Hi adam.... what you suggested is roughly what I did with my if statement anyway... the problem seems to be that sometimes it doesn't exist as an object even when a window has opened mysite in a new window! For some referrers this action results in the answer and for others doing exactly the same thing I get an error... the sites do exactly the same action so I am confused as to why my call to the object results in an error.....
cheers Mike
 
Mike,

You might try to use,

if(exists(window.opener))

to test for the object.

tsmith
 
won't solve my problem... you see I know the opening window exists as it sat there on my desktop in front of me... the problem is all the code I've tried says it doesn't exist (but only in these few instances) and I can't access the objec tof this window, it clearly does exist so can anyone tell me why I can't access it just 'some' times? Thanks Mike
 
I think we should first clarify how referrer works.

When a user navigates to a destination document by clicking a Link object on a source document, the referrer property contains the URL of the source document. Evaluate the referrer property from the destination document.

If the user clicked on a link to get to the current URL, referrer contains the URL the user linked from.
referrer is empty if the user typed a URL in the Location box, or used some other means to get to the current URL.
referrer is also empty if the server does not provide environment variable information.

Taken from original Netscape's Javascript guide.
 
starway, thanks for the comments, could you provide a link to the information you refer to?
"referrer is also empty if the server does not provide environment variable information.
Taken from original Netscape's Javascript guide."

but :) should this however prevent me from accessing window.opener.location e.g. the url held in the window that opened the new window? It appears to me that window.opener is not valid and I can't relate this to the server at all as it is a client side action call from one window directly to the next..... I'm still confused.
thanks mike
 
I always use an older (but still good) offline version of this:

Read about referrer property here:

>>should this however prevent me from accessing window.opener.location e.g. the url held in the window that opened the new window?
This has nothnig in common with referrer. Just use this:
var a = window.opener.location.href;
 
thanks alot for the links...

I can't use window.opener.location.href;
because any ref I make to window.opener results in an error saying that window.opener is NULL or not a valid object (this only happens with some sites tho'), I test ( if (window.opener) )before I run any ref to it but now my code doesn't run because it says its not there also.... BUT it clearly is there I can see the window that opened the new window plain as day... why can't I reference it? thanks Mike
 
Is your site being opened from the referring site via JavaScript or by right-clicking a link and opening a new window? If it's the latter, I don't think the window.opener object will exist.
 
the window is opened by form drop down submission. if I run the same code (from same domain as me) I get the object if I run it from the other domain I don't.

i have know problem picking up onclick open window or right click open window from another domain.

thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top