Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Apr 2005
    Location
    San Diego, CA
    Posts
    107
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Search within a string

    Hi,

    I'm trying to determine if a referrer is from a domain.

    So, for example, page http://sumeetjain.com/money.htm is linked to from http://sumeetjain.com/a.htm, http://sumeetjain.com/b.htm, and http://sumeetjain.com/c.htm.

    I can do document.referrer; to grab the referrer to money.htm, but I just need to know if the referrer is from the domain sumeetjain.com. It doesn't matter if it's from a.htm, b.htm, or c.htm.

    Is it possible to store the full referrer into a variable and then check if that variable's value begins with "http://sumeetjain.com" or if it contains "sumeetjain.com"? That check would let me know if it's from the domain.

    Or if you can think of another way to accomplish this, that'd be great to. I appreciate any help you all can provide.

    Thanks,

    Sumeet

  • #2
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could use indexOf() or you could use Regular Expressions.

    Code:
    if(document.referrer.indexOf('sumeetjain.com')!=-1)
    http://www.devguru.com/Technologies/...g_indexof.html
    http://www.quirksmode.org/js/strings.html
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,590
    Thanks
    0
    Thanked 644 Times in 634 Posts
    Just remember that a lot of people have document.referrer disabled in their browser or firewall so that a lot of the time you will get blank passed instead of the address of the referrer page.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Regular Coder
    Join Date
    Apr 2005
    Location
    San Diego, CA
    Posts
    107
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for the help - both of you. The method worked perfectly, and I'll be sure to remember the limitations of the technology.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •