DelphiFAQ Home Search:
General :: Programming :: JavaScript
Code snippets to add functionality to your HTML pages, preferably cross-browser.

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.

Featured Article

Dynamic content based on referrer (using client side javascript)

Question:

How do I detect the referrer in order to display the appropriate content?

Answer:

Such things should really happen on the server side. The reason is that some browsers may not support javascript (Lynx) or the user may have disabled javascript for security reasons.

However, if you have a static HTML page and it is beyond your control to change this into a server side parsed document (typically by renaming it to .SHTML) and you cannot change the web server's configuration to do parsing on HTML pages.. and you cannot use php or jsp.. only in this situation it is justified to use javascript for content selection.

The example does a redirect depending on from which page your request came. The referrer is a property of the document and indexOf() is used to check for a relevant substring.

<script language="JavaScript">
 <!--
 if (document.referrer.indexOf('url1.com') > -1)
     location.href = 'index1.html';
 else if (document.referrer.indexOf('url2.com') > -1)
     location.href = 'index2.html';
 else
    location.href = 'index0.html';
 //-->

 </script>
 

Generated 20:02:16 on Oct 18, 2018