Jump to content

document.referrer


manick
 Share

Recommended Posts

Hi all,Sorry about this, we did have a thread going on this, and thought it was sorted, however I now have managed to test the following script on a live server and it just keeps opening up the index.html page, regardless of where it opens the pages from. Any help much appreciated as to where I am going wrong:function checker(){var x=document.referrerif ((x=="main.html")||(x=="wishlist.html")||(x=="prodblank.html")||(x=="form.html")||(x=="product1.htm")||(x=="index.html")){ return true} else {window.location="index.html";return false} }many thanksNick Trenam

Link to comment
Share on other sites

it's this line:if ((x=="main.html")||(x=="wishlist.html")||(x=="prodblank.html")||(x=="form.html")||(x=="product1.htm")||(x=="index.html")){ The referrer always returns the FULL url, but you're only equating to a little bit of it.Try:if(x.match(/(main|wishlist|prodblank|form|product1|index).htm/i) != null){

Link to comment
Share on other sites

Any help much appreciated as to where I am going wrong:
referrer is as a whole URL, so we need to split it a smaller bits.
function checker(){var x = document.referrer.split('/');x = x[x.length - 1];x = x.split('?'); // just in case there is query incoming if (x == "test.html" ||     x == "wishlist.html" ||     x == "prodblank.html" ||     x == "form.html" ||     x == "123.html" ||     x == "index.html"){    return true; } else {  window.location = "index.html";  return false;  } }

Cleaned up it a bit, and remover unneeded () and added couple ; -marks.This line splits URL to the array via / -marks of URL:var x = document.referrer.split('/');this line takes last element of array and puts it as a filename:x = x[x.length - 1];tested with live server, and works.edit:Blue was there before me, I was late again, but so what? :)

Link to comment
Share on other sites

Hey all,Thanks for that, am i to assume that this must have a full url to work i.e. www.whatever.com/whatever, the reason i ask is because unfortunately all i have ot work with is a personal area on the Open University site that starts http://ouserver/myname/page.html. The reason I ask is because i have tried both of the ways you 2 have ssuggested but it still does the same thing.Raimo with regard to your code what do the following lines of code do:var x = document.referrer.split('/');x = x[x.length - 1];x = x.split('?')many thanks again for your help.Regards nick

Link to comment
Share on other sites

Raimo with regard to your code what do the following lines of code do:var x = document.referrer.split('/');x = x[x.length - 1];x = x.split('?')
I'd explain it at the bottom of my post, but I can do it again, no problem: :) We have referrer: http://w3schools.invisionzone.com/not_exis...ct=Post&CODE=02var x = document.referrer.split('/'); splits referrer from every / - mark to the array.so we have x called array who is eatting these stuff:x[0] = http:x[1] = x[2] = w3schools.invisionzone.comx[3] = not_existsx[4] = index.php?act=Post&CODE=02and then x = x[x.length - 1]; takes always the last one (x[4] in this case) to handle as a filename, andx = x.split('?') slits away ? and rest of query part of URL, so now we have x = index.phpLast one (? split) can be "not needed", but I'm not sure how all browsers handles referrer,so it's there just in case.If it still not working, look first the referrer with alert(document.referrer); and thenwith alert(x); what is happening every part of the script.Anyway, I'd test it with my live server and it works OK. :)Big help is if You use Firefox, there is Tools -> Javascript Console (or Error console) try to catch errors whit it. Opera console: Tools -> Advanced -> Error consoleRemember, there is always someones (etc Opera users in very easy way) who are blocking to show referrer, so this kind of referrer-sniffing-system is not fullproof, not at all.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...