[Solved] What is the code meant? [closed]


When a user to your website, say:

http://example.com/

Now, the document.referrer is "" (blank) as it was not “referred” by any link. The user typed it. The document.referrer holds the link from which the page has been opened.

Now, when the home page has a link like http://example.com/signup, and the user clicks it, and goes to the page, and the page has the following code:

document.referrer; // This would give http://example.com/ as the referrer.

When you are checking this:

referrer.indexOf(location.host) == -1

What exactly happens is:

"http://example.com/".indexOf("http://example.com/") == -1

Where, both have the content. This shows that the link has been clicked from the local page available in the same domain. When the referrer is not the local page, then add the referrer in the cookie and do some process is what the if condition does.

There are a lot of conditions in the code. They are the cases from where the user might have come from.

Case 1: User has not come from anywhere. It returns undefined. You will get 2 logged in the console.

Case 2: If he comes from a different page, You will get 3 logged in the console.

Case 3: If he comes from the same page, You will get 4 logged in the console.

solved What is the code meant? [closed]