Jump to content

document.referrer question


manick

Recommended Posts

Hi all,I am a total novice and need help please!, I am rubbish at JS but need to get a paper in on it in a couple of weeks.I am trying to do a piece of script that checks to see if a user has entered in through the home page. Basically in my script I am trying to say has the user linked from one of the pages in the site, if not then a boolean is false and the user is redirected to the main page (index.html)Here is the code I have come up with, I appreciate it is probably way out but I can't test it! any help VERY much appreciated!!External.jsvar beenbefore =new Boolean function beenbefore(){if (document.referrer = "code.html||main.html||wishlist.html||prodblank.html||form.html||product1.htm") return beenbefore(true)}anypage.htmlbeenbefore(){if beenbefore = false}window.location(index.html)Nick Trenam

Link to comment
Share on other sites

Hi Nick, your code should look something like this. You weren't to far off, i've just tidied it up here and there.You only need one function, all your pages were one long string and you were assigning that value to the referrer.Hopefully this should do what you want. :)

<head><script>function checker(){var x=document.referrer;  if ((x=="code.html")||(x=="main.html")||(x=="wishlist.html")||(x=="prodblank.html")||(x=="form.html")||(x=="product1.htm"))   {     alert("You came from another page in my site");  }  else  {    alert("You didn't come from another page in my site");    window.location="http://www.google.com";  }}</script></head><body onload="checker()"></body>

Link to comment
Share on other sites

ahhh slick coding blue, yes that's brill, saves testing each individual page, just the domain name. Plus if you added any other pages you wouldn't have to update the if statement. lovin' it :)

Link to comment
Share on other sites

Thanks guys,It was good to know that i wasn't too far off as well, maybe i do know slightly more than I thought (not much though!!). This is for an assignment which won't be going on a live site, so I won;t be able to check it, but as you guys know what yo are doing, I'll stick it on as is.Again, many thanksNick Trenam

Link to comment
Share on other sites

It's worth testing it out yourself rather than just putting it in - or you won't see the results :)

<script type='text/javascript'>if( document.referrer.match(/mysite.com/i) != null){  document.write('You came from a page on my site');} else {  document.write('You came from somewhere else');}</script>
Link to comment
Share on other sites

The opposite, case insensitive (hence the i :)). Just in case you type in MYSiTe.CoMthe JS wouldn't recognise it then.. but this way, it recognises upper, lower and mixed case :)gi = global insensitive. There's also m for multi-line.. although it's hardly used.

Link to comment
Share on other sites

The opposite, case insensitive (hence the i :)). Just in case you type in MYSiTe.CoMthe JS wouldn't recognise it then.. but this way, it recognises upper, lower and mixed case :)gi = global insensitive. There's also m for multi-line.. although it's hardly used.

I really need to brush up on...it is regular expressions, right?
Link to comment
Share on other sites

Hi again,Ok that works when added to the page, my next (REALLY basic) question is this, I am using this on several pages (obviously), I can;t seem to find anywhere on teh web how to call to more than one ecternal JS file at a time, is it anything like this???<script src="linkrollover.js";"checker.js" language="javascript" type="text/javascript"> window.onload="checker()"i.e is it a semicolon to seperate them or something else?? I appreciate this is a basic question but I just cannot find the answer.I'm assuming it's not cos it aint working!!Again, any help much appreciated.nick trenam

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
×
×
  • Create New...