CancelImage Upload

How to determine a website visitor's origin in JavaScript

In order for your website to become a success you may need a good idea about where your user is coming from. With the following JavaScript code you will easily find out what the last destination was, and how they came to you. A great method therefore for showing who's linking to you.

Note that in order for you to store this variable you may want to combine this code with some server side programming, or maybe even looking up the server side programming language's variation of this code. Here for instance is a tutorial on how it is done in PHP, very much comparable to the JavaScript method:

var referrer = document.referrer;

If you want to test this code here is a more complete example. Remember that nothing will be displayed unless you actually write another page with a link to this one:

<html>
<head>
<title>How to determine a visitor's origin in JavaScript</title>
<script language="JavaScript">

function getReferrer()
{

        var spanReferrer = document.getElementById("referrer");

        var referrer = document.referrer;

        if(referrer != "")
        {

                spanReferrer.innerHTML = "Referrer: " + referrer;

        }
        else
        {

                spanReferrer.innerHTML = "You have come to this page directly.";

        }

}

</script>

</head>
<body onload="javascript:getReferrer();">

<span id="referrer"></span>

</body>
</html>


Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com