Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript pathname referrer

How can do the same function in javascript that is document.location.pathname - except with the referrer? so something like document.referrer.pathname?

Thanks.

like image 306
Matt Avatar asked Jun 29 '11 20:06

Matt


People also ask

What is pathname in Javascript?

The pathname property of the Location interface is a string containing the path of the URL for the location, which will be the empty string if there is no path.

What is referrer in JS?

The referrer property returns the URL of the document that loaded the current document.

How do I find pathname location?

window.location.pathname returns the path and filename of the current page. window.location.protocol returns the web protocol used (http: or https:) window.location.assign() loads a new document.

How do I access pathname?

The Location pathname property in HTML is used to set or return the pathname of a URL. The Location pathname property returns a string which represents the pathname of the URL. Syntax: It returns the pathname property.


2 Answers

No, you can only extract needed part manually:

document.referrer.replace(/^[^:]+:\/\/[^/]+/, '').replace(/#.*/, '')
like image 134
RReverser Avatar answered Oct 20 '22 23:10

RReverser


You can extract the pathname from the document.referrer and parse it with new URL() with the following code

const url = new URL(document.referrer)
url.pathname

Be sure to polyfill URL for IE 10 and below, easily done with https://polyfill.io/v3/polyfill.js?features=URL

like image 5
kylewelsby Avatar answered Oct 20 '22 23:10

kylewelsby