Zoolander
Zoolander

Reputation: 2363

How to grab referring URL without the hostname in Javascript?

I need grab the path of a referring URL, which can be served from many different hostnames which can be under HTTP or HTTPS and even have port numbers.

Can someone give me an example of how to run some code only if the referring URL contains a specific string such as path and a file name or just a certain path without a file extension?

Upvotes: 0

Views: 2756

Answers (1)

deviousdodo
deviousdodo

Reputation: 9172

Well, you can start with something simple:

var parts = document.referrer.replace(/^https?:\/\//, '').split('/');
parts.shift();
var path = parts.join('/');
if (path.indexOf('filename') > -1) {
    // code here
}

And if you need more flexibility see this library: http://blog.stevenlevithan.com/archives/parseuri

Upvotes: 4

Related Questions