Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript - get domain ONLY from document.referrer

I would like to get ONLY the domain from the referrer urls. The referrer urls I mostly get are http://www.davidj.com/pages/flyer.asp & http://www.ronniej.com/linkdes.com/?adv=267&loc=897

Whenver I get referrer urls like the above, I just want to get the domain example: http://www.davidj.com

I have tried using the .split method but i am having trouble using it.

like image 466
Stephen Avatar asked Apr 25 '12 13:04

Stephen


People also ask

How do I get Referer in Javascript?

However, since you can use javascript on website2, you could get the referrer ( document. referrer ) and add it to the url of the pixel you get. For example: var referer = document.

What is document referrer Javascript?

document. referrer gives you the URI of the page that linked to the current page. This is a value that's available for all pages, not just frames. window. parent gives you the parent frame, and its location is its URI.

How do I find an old URL?

If you want to go to the previous page without knowing the url, you could use the new History api. history. back(); //Go to the previous page history. forward(); //Go to the next page in the stack history.go(index); //Where index could be 1, -1, 56, etc.


2 Answers

var url = "http://www.ronniej.com/linkdes.com/?adv=267&loc=897"
var referrer =  url.match(/:\/\/(.[^/]+)/)[1];

http://jsfiddle.net/hyjcD/

if (document.referrer) {
   url = document.referrer; 
   ref = url.match(/:\/\/(.[^/]+)/)[1];
}
like image 190
undefined Avatar answered Sep 21 '22 05:09

undefined


you can use internally write the url to an anchor element and from that one get the smaller parts

var anchor = document.createElement("a");
anchor.href = "http://www.davidj.com/pages/flyer.asp";

console.log(anchor.protocol + "//" + anchor.host); // "http://www.davidj.com"

it is far easier then as you do not have to take care about splitting or something like that... it is quite logical... the native anchor has the same properties like window.location at least regarding the URL

EDIT: IE 6-9 adds the default port to anchor.host // "http://www.davidj.com:80

like image 37
Tobias Krogh Avatar answered Sep 20 '22 05:09

Tobias Krogh