Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Extract the Current URL and ID in jQuery?

How would I go about getting the current URL using jquery, or more specifically, getting an ID on the end of it?

For example, I have product.php#tab-2. What I want to get from it is just the '#tab-2' part.

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

Thanks

like image 971
Probocop Avatar asked Dec 02 '25 01:12

Probocop


1 Answers

You don't 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 the Mozilla docs at window.location - MDC.

like image 52
Andy E Avatar answered Dec 03 '25 19:12

Andy E



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!