manick Posted March 22, 2006 Share Posted March 22, 2006 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 More sharing options...
scott100 Posted March 22, 2006 Share Posted March 22, 2006 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 More sharing options...
Webworldx Posted March 22, 2006 Share Posted March 22, 2006 Won't you want to .match() that scott - the referrer shows the full URL doesn't it? Link to comment Share on other sites More sharing options...
scott100 Posted March 22, 2006 Share Posted March 22, 2006 Won't you want to .match() that scott - the referrer shows the full URL doesn't it?<{POST_SNAPBACK}> ah yes blue your correct either match: http://www.w3schools.com/jsref/jsref_match.aspor add http etc if ((x=="http://www.mysite.com/code.html") Link to comment Share on other sites More sharing options...
Webworldx Posted March 22, 2006 Share Posted March 22, 2006 In fact, if you do a:if( x.match(/mysite.com/i) != null){ //The last page visited was on the site}That'd do for it wouldn't it? Link to comment Share on other sites More sharing options...
scott100 Posted March 22, 2006 Share Posted March 22, 2006 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 More sharing options...
manick Posted March 23, 2006 Author Share Posted March 23, 2006 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 More sharing options...
Webworldx Posted March 23, 2006 Share Posted March 23, 2006 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 More sharing options...
manick Posted March 23, 2006 Author Share Posted March 23, 2006 Ok will do, sorry for being a thicket but what part does the "i" play in the matchpart of the script after "mysite.html/" ?? Link to comment Share on other sites More sharing options...
scott100 Posted March 23, 2006 Share Posted March 23, 2006 I'm pretty sure it makes it case sensitive Link to comment Share on other sites More sharing options...
Chocolate570 Posted March 23, 2006 Share Posted March 23, 2006 Scott is correct. i means case sensitive, g means global, and from what I can remember, gi means something too. Link to comment Share on other sites More sharing options...
Webworldx Posted March 23, 2006 Share Posted March 23, 2006 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 More sharing options...
Chocolate570 Posted March 24, 2006 Share Posted March 24, 2006 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.<{POST_SNAPBACK}> I really need to brush up on...it is regular expressions, right? Link to comment Share on other sites More sharing options...
Webworldx Posted March 24, 2006 Share Posted March 24, 2006 Yup. Equivalent ofnew RegExp("mysite.com" , "i"); Link to comment Share on other sites More sharing options...
manick Posted March 24, 2006 Author Share Posted March 24, 2006 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 More sharing options...
scott100 Posted March 24, 2006 Share Posted March 24, 2006 try this:<script language="JavaScript" src="yourfile.js"></scritpt><script language="JavaScript" src="anotherfile.js"></scritpt> Link to comment Share on other sites More sharing options...
manick Posted March 24, 2006 Author Share Posted March 24, 2006 Thanks again scott Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now