Capturing URL parameter with jQuery

Asked

Viewed 34 times

0

Current I am capturing the URL as follows:

$(this).attr('href')

Let’s assume the URL is: http:/test.com/login

How do I capture only the end: /login

1 answer

2

If you want to take only the last "string" ('login') you can use the split('/') property that will transform the URL into an array based on the/' used by it, and from this, you can take the last position of this array.

var URL = 'http://teste.com/login';
var URLArr = URL.split('/'); // ["http:", "teste.com", "login"]
var URLPath = URLArr[URLArr.length -1]; // "login"

Or,

can use replace

$(this).attr('href').replace(/^.*\/\/[^\/]+/, '');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.