How to obtain anchor part of URL after #
in JavaScript? This is related with How to obtain anchor part of URL after # in php
Asked
Active
Viewed 3,753 times
3
-
1@santanu, please edit the title of your question to make it more specific. – Rahul Jun 24 '09 at 05:40
2 Answers
9
To get the "anchor part" of the current page, you can use:
var hash = window.location.hash;
Then, based on your question link, you want to send it to a PHP script. You can do this with a JavaScript redirect like so:
window.location = "myscript.php?hash=" + encodeURIComponent(hash);
However, this won't work for users without JavaScript enabled, so be sure to have a <noscript>
message ready!

MiffTheFox
- 21,302
- 14
- 69
- 94
0
You could use a regular expression:
var url = "http://nhs/search-panel.php#?patientid=2";
var hash = url.match(/^[^#]*#(.*)/)[1];