Link to home
Start Free TrialLog in
Avatar of pnreefman
pnreefman

asked on

document.referrer and click throughs...

gday all :O)
im slowly getting around to having a clickthrough recorder on my site (www.portlandnow.net.au), and this question is to help me set it up.
now i think that to have a working click through meter, i need to do the following... 1: find out where the user came from. 2: send the data to a cgi recording program. 3: access the data in the cgi program at the end of the month to see how many clicks ive had...
the first point is my question. ive been playing around with history.previous, and document.referrer, both of which arent working for me. any suggestions?
p.s. the site is frames based, so would that mean that the previous page is the frameset page, and the true previous page is one before that?
the bottom line question is... how do i display (and then record) the user's previous page?...
thanx in advance
Peter Reefman
Avatar of rafistern
rafistern

1. IE doesn't allow you to access document.referrer . I presume this is a security feature.
2. The referrer should be the page within the frameset, not the frameset.
rafistern makes a good point.  This sounds like the perfect case for a web-server log parsing script for perfect accuracy, which is beyond javascript and would require you starting from scratch.  Doesn't sound too appetising.

Have you tried either of these techniques:

1) instead of using document.referrer (which gives you the frameset), use top.document.referrer (since top is the frameset).

2) place the following script in the frameset document:

<script>
<!-- conceal

var Referrer = document.referrer;

// reveal -->
</script>

.and then in your counter script, instead of calling document.referrer, call top.Referrer (which is a variable).


I haven't tested these in multiple browsers, so I didn't submit as an answer.  But I'd be curious to know how it turns out.

Cheers.
-Quixote
Avatar of Michel Plungjan
Q: It should work in all js enabled browsers but IE3

PNReef: It must be loaded from a link. Referrer is not set in IE from direct typing in location or from harddisk.

Michel
Avatar of pnreefman

ASKER

That works beautifully in IE4.0 Quixote...
I'll try it in a few other (NN3, NN4, IE3) browsers to see how it goes.

If you use the HTTP_REFERER environment variable server-side you will not necessarily get what you want. If you have a counter script embedded in an html page then the HTTP_REFERER is the page in which it is embedded. If the page is a cgi script itself then the HTTP_REFERER will be the page from whence you came.
rafistern-

Your point is exactly why much of the time this sort of thing is done client-side, using javascript.  By using the document.referrer property of the page, one can dynamicly create an image call like:

<img src="http://www.foo.com/cgi-bin/counter.cgi?referrer=www.bar.com">

And then your counter.cgi can know where the visitor came from (www.bar.com) as well as what page the script is being called from by measuring HTTP_REFERER on the server.  Things can get pretty powerful quickly as soon as you combine client-side work with server-side functionality.

Cheers.
-Quixote
Well, I don't know why you need so sophisticated way :)
Isn't it easier to have

<HTML>
<FRAMESET rows="50%,*,1">
 <FRAME NAME="left"   SRC="" > 
 <FRAME NAME="right"  src="kaka.html">  
 <FRAME NAME="hidden" src="/cgi-bin/mylog.pl" FRAMEBORDER="NO" NORESIZE SCROLLING="NO" MARGINHEIGHT=1>
</FRAMESET>
</HTML>

So when someone fall into your frameset
mylog.cgi will be called, this CGI can get
HTTP_REFERRER variable and log it.

In case you want to display user where he came from
you can use in any frame

<script>
 document.writeln(top.document.referrer)
 // oh well, you can call your log cgi here instead
 // of calling from frameset, but this is wired
 // something like this:
 document.writeln('<IMG SRC="/cgi-bin/logit.cgi?ref='+escape(top.document.referrer)+'" BORDER=0 WIDTH=1 HEIGHT=1>');
</script>

Well this script will call CGI for image source,
so your CGI should return image for it, it easy to do
using simple relocation in Perl

print <<EOF;
Location: http://www.yourserver.com/someimage.gif

EOF
# make your log here geting it from QUERRY_STRING


Some differences:
Last solution will makes log each time
user access the document which contains script
In this case inner frame.
(Imagine, user browse several documents at your site
 and turns back to to entry point
)

The way of calling script by loading in third frame (frameset solution) will call log script only once when frameset loaded
I don't know what exactly you want to log,
all document loadings, or only when user comes to your site.
So this is up to you to choose.

Last thing..
Guess you have no rights to run your OWN scripts, and trying to use existing ones like mail.cgi existing scripts.
In this case you can make form in the hidden frame instead
<form ...>
 ...some form fields here...
</form>
than call script
</script>
 //set some field to referrer
 document.forms[0].elements[0].value=""+top.document.referrer;
 document.forms[0].submit();
</script>

In case you put this form in hidden frame, the script
output doesn't matter :)

Limitations:
Calling log as FRAME SRC="/cgi-bin/yourlog.cgi"
Will work in ANY browser which at least support frames;
And don't require JavaScript at all.
(at least for logging)

Using JavaScript is limited to NN3,NN4,MIE4
Sorry, as I remeber MIE3 is nightmare...
I have no MIE3 to check, but I guess it don't support .referrer.

kollegov,
Your frame solution will work fine in IE, but in NS the HTTP_REFERER of the framed page is the frameset.
rafistern, I just checked your comment, and found that
NN3 and NN4 gives referrer (previous document)
and MIE gives frameset page. Just opposite to your post :)
But you right, MIE as usual deliver more problems :)

So I just make the following solution:

In frameset I have 'hidden' frame
<HTML>
<FRAMESET rows="50%,*,20">
 <FRAME NAME="left"   SRC="" > 
 <FRAME NAME="right"  src="kaka.html">  
 <FRAME NAME="hidden" src="logger.html"
FRAMEBORDER="NO" NORESIZE SCROLLING="NO" MARGINHEIGHT=1>
</FRAMESET>
</HTML>

and this is logger.html
<html>
<script>
function logit()
{document.location="/cgi-bin/vmax/test.cgi?ref="+escape(top.document.referrer+"");
}
</script>

<body>
</body>

<script language="JavaScript1.1">
logit();
</script>
</html>

This pair was tested with NN3, MIE4, NN4
And shouldn't give errors in MIE3
(Sorry have no MIE3 to check:)


You're right, I was posting from memory (a bad memory :-)) which my subconcious had obviously subverted..... :-)
AFAIK IE3 always returns current page as referrer from JavaScript.

Michel
AFAIK IE3 always returns current page as referrer from JavaScript.

Michel
ASKER CERTIFIED SOLUTION
Avatar of kollegov
kollegov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yeah thats fine. :O)
thanx for all that everyone.