Check url of referer and print a message

Discussion in 'JavaScript' started by Dragma, May 6, 2013.

  1. #1
    Hi all!

    I have been trying to find how to get the refering url to a page of my website, check if it's included on a txt file and then print a modal popup with a message. So, if you come from a friend url included on the txt it will welcomes you "You come from a friend's url" for example, and if not, then another msg will be printed.

    Someone knows? I have found codes for the different functions separately but don't know how to put them all together to do this.

    Thanks!
     
    Dragma, May 6, 2013 IP
  2. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #2
    You need to use:

    
     
    var referrer = document.referrer;
    
    Code (markup):

    If you want to get the domain name use:

    
    var referrer = document.referrer.split('/')[2];
    
    Code (markup):
     
    HuggyStudios, May 6, 2013 IP
  3. Dragma

    Dragma Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Hi! thanks for answering.
    I think I would need something like this :
    <?php
    if (ref.match(/^https?:\/\/([^\/]+\.)?friendsite\.com(\/|$)/i)) {
      alert("You came from a friend Website");
      else { alert("You came from a non friendly Webstie");
    }
    ?>
    Code (markup):
    But this is just not working at all as it gives me a 500 error. Thanks for helping!
     
    Dragma, May 6, 2013 IP
  4. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #4
    Don't wrap the code in PHP tags, it's JavaScript.

    
    <script>
    if (ref.match(/^https?:\/\/([^\/]+\.)?friendsite\.com(\/|$)/i)) { 
    alert("You came from a friend Website");
    } else {
    alert("You came from a non friendly Webstie");
    }
    </script>
    
    Code (markup):
     
    HuggyStudios, May 7, 2013 IP