How to extract current url and id in jQuery?

How can I get the current url using jquery or, more specifically, get the id at the end of it?

For example, I have product.php # tab-2. I want to get from him only part of "# tab-2".

I tried 'window.location.pathname', but that will return '/product.php'

thank

+3
source share
4 answers

You do not need jQuery for this:

alert(window.location.href); // will give you the full url
alert(window.location.hash); // will give you the hash (#) value

See Mozilla window.location - MDC docs .

+7
source

Do you want window.location.hash

+1
source

jqUrl (http://www.oakcitygraphics.com/jquery/jqURL/jqURLdemo.html), URL-, window.location.pathname.

0

, :

var url = window.location.href;

var id = url.substring(url.lastIndexOf('#') + 1);

Please note that if your url is in this format /product.php/3, then you can use the code above, just change the character as a function of lastIndexOf.

0
source

Source: https://habr.com/ru/post/1733187/


All Articles