View Full Version : document.referrer offline
Graeme Hackston
07-20-2002, 07:05 AM
I made the function below to test if I could determine whether there is atleast one URL in the browsers history. If there is a URL, I want to show a javascript back button. If not, I want to hide it.
It only works if I upload it. This is the test page (http://members.rogers.com/hackston/Referrer_Link_Test.htm). When I test it locally, it document.writes "Null".
Is there a syntax error in the function or can this, for some reason, only work online?
If it only works online is there another method of detecting that the browsers has atleast opened one other page before opening my page (regardless of whether or not it is online)?
<script>
onload = function() {
if (document.referrer != '') {
document.write("You just visited "+document.referrer)
} else {
document.write("Null")
}
}
</script>
Thanks for your help.
joh6nn
07-20-2002, 08:11 AM
there are some specifics to how document.referrer works, but i don't know what they are. in my experience, document.referrer will only work properly if it's online, and if you click a link.
you might try possibly using history.length, though i'm not sure that this will work everywhere, or at all.
<script>
onload = function() {
if (history.length > 0) {
document.write('<a href="javascript:history.back();">Return to last page</a>') :
} else {
document.write("Null")
}
}
</script>
don't know if that's what you were looking for, but it was the best i could come up with at 3 in the morning. maybe someone out there knows the specifics of document.referrer better than i do.
Graeme Hackston
07-20-2002, 03:21 PM
Thanks joh6nn, that's a better approach. The only issue I’ve got now is making it work cross browser. Mozilla 1.0 and NN4.74 alert “1 page(s)” when opening this test page (http://members.rogers.com/hackston/Referrer_Test1.htm) in a new browser session while IE6 and Opera 6 alert “0 page(s)”.
<edit>
Here is the test page code:
<html>
<title></title>
<body>
<script>
alert(history.length+" page(s)");
</script>
</body>
</html>
</edit>
Graeme Hackston
07-20-2002, 03:50 PM
I think I've made a browser work around here (http://members.rogers.com/hackston/Referrer_Test2.htm) but I still need to detect if the history is back() or forward()
Here is the page code:
<html>
<title></title>
<body>
<script>
onload = function() {
if ((document.layers) || (document.getElementById && !document.all)) {
alert(history.length-1+" page(s)");
} else {
alert(history.length+" page(s)");
}
}
</script>
</body>
</html>
<edit>
Does anyone know why this is necessary? Does Mozilla count the first page as page 1?
</edit>
Graeme Hackston
07-21-2002, 01:22 PM
This is as good as I've been able to get it. It works enough for me to use it but also reads forward history as history.length in every browsers I've tested it in but Opera 6.
Opera must read history.length as back history only because it works as I intended it.
<html>
<head>
<title></title>
<script>
onload = function() {
if ((document.layers) || (document.getElementById && !document.all)) {
var pages = history.length-1;
} else {
var pages = history.length;
}
if (pages != 0) {
alert("history.length = "+history.length+" page(s)")
} else {
alert("Null")
}
}
</script>
</head>
<body>
</body>
</html>
Graeme Hackston
07-22-2002, 11:47 AM
Just a bump to see if anyone might know a better way to do this.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.