Capturing URL parameter with jQuery

0

I'm currently capturing the URL as follows:

$(this).attr('href')

Let's suppose that the URL is: http: /teste.com/login

How do I capture only the end: / login

    
asked by anonymous 24.10.2016 / 02:06

1 answer

2

If you want to get 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, You can get 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,

You can use replace

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