www.webdeveloper.com
Results 1 to 6 of 6

Thread: Redirect if coming from a external webpage

Hybrid View

  1. #1
    Join Date
    Jan 2013
    Location
    NC, USA
    Posts
    4

    Exclamation Redirect if coming from a external webpage

    I need my users that come from a specific external website to be redirected to a page in my site once they click a button.

    Example:
    1. User clicks link on a page in site A leading to site B
    2. Once there, user clicks a link that will send only users that came from site A to the specific pages.
    3. All other users that did not come from site A will be redirected to another page from the users that did.



    THIS NEEDS TO BE IN STRICTLY JAVASCRIPT AND HTML ONLY.

  2. #2
    Join Date
    Nov 2010
    Posts
    1,057
    do you have control over the external website as well?

  3. #3
    Join Date
    Jan 2013
    Location
    NC, USA
    Posts
    4
    yes

  4. #4
    Join Date
    Sep 2008
    Location
    Akron, OH
    Posts
    1,112
    Here's an example:

    HTML
    Code:
    <body onload="onload()">
    	<a href="defaultlink.html" id="theLink">Link</a>
    </body>
    JavaScript
    Code:
    <script type="text/javascript">
    function onload() {
    				if (document.referrer) {
    					// gets full URL, ex: http://www.site.com/directory/1/2/index.html
    				   url = document.referrer; 
    				   // just gets the domain, ex: www.site.com
    				   ref = url.match(/:\/\/(.[^/]+)/)[1];
    				   
    				   // alert for testing
    				   alert(ref);
    				   
    				   // if the ref equals what you want, then change the link href below to the alternate link
    				   if (ref == 'www.google.com') {
    					 var link = document.getElementById('theLink')
    					 link.href = 'alternatelink.html';
    				   }
    				}
    			}
    </script>
    In this example, if they come from www.google.com, the link will be updated to the alternatelink.html. Otherwise, it goes to the default link.
    I'm always up for networking with fellow web professionals. Connect with me on LinkedIn if you like!

  5. #5
    Join Date
    Jan 2013
    Location
    NC, USA
    Posts
    4
    Is this cross-browser compatible?

    Quote Originally Posted by cbVision View Post
    Here's an example:

    HTML
    Code:
    <body onload="onload()">
    	<a href="defaultlink.html" id="theLink">Link</a>
    </body>
    JavaScript
    Code:
    <script type="text/javascript">
    function onload() {
    				if (document.referrer) {
    					// gets full URL, ex: http://www.site.com/directory/1/2/index.html
    				   url = document.referrer; 
    				   // just gets the domain, ex: www.site.com
    				   ref = url.match(/:\/\/(.[^/]+)/)[1];
    				   
    				   // alert for testing
    				   alert(ref);
    				   
    				   // if the ref equals what you want, then change the link href below to the alternate link
    				   if (ref == 'www.google.com') {
    					 var link = document.getElementById('theLink')
    					 link.href = 'alternatelink.html';
    				   }
    				}
    			}
    </script>
    In this example, if they come from www.google.com, the link will be updated to the alternatelink.html. Otherwise, it goes to the default link.

  6. #6
    Join Date
    Jan 2013
    Location
    NC, USA
    Posts
    4
    Also, how can I change this to make it work with buttons instead of links?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles