0

I am wanting to set up a javascript to accomplish the following:

A user opens up a bookmarked webpage and the script checks whether or not it came from a referring domain (say, site.com) and if not redirects them automatically to anothersite.com. The goal is so that only when users come from the anothersite.com site will the site.com page come up. Hope this makes sense.

I would normally use PHP for this but can't for this project so we're stuck doing this with javascript.

WebPhenom
  • 49
  • 1
  • 1
  • 3

2 Answers2

0

Look into document.referrer to get the referrer's URL, extract the domain from it and then validate.

Eugene Naydenov
  • 7,165
  • 2
  • 25
  • 43
  • So here is what I am wanting to do, does this make sense. User goes to a page and there is code that first checks if the user came from a referring domain and if not, redirect them to another page. But if the user did come from the referring domain dont redirect them. So would this work so that anyone that had previously book marked the page, when the page loads, because the did not come from the referring domain they would be kicked out to the redirected site? – WebPhenom Aug 16 '12 at 18:30
  • You're right, if user opens the site from bookmarks your JavaScript will redirect him since the `document.referrer` is not set in this case. You can store the information somewhere in localStorage or cookies saying that this user is allowed to open this site from blank page and check it before make decision should you redirect him or not. But this will work till user will delete cookies, reinstalled browser, etc. So this solution is not reliable though. – Eugene Naydenov Aug 16 '12 at 19:14
0

I didn't quite get the part of "opens up a bookmarked" since it has been said that knowing that much is not easily accomplished Is there a way to know if someone has bookmarked your website?

..but to inspect where it came from I would look into How do you get the previous url in Javascript?

plus then make your own if else stuff and use window.location.href = "your_site_url"; to direct him the page you wanted, job done!

Community
  • 1
  • 1
Mauno Vähä
  • 9,688
  • 3
  • 33
  • 54