3

How to obtain anchor part of URL after # in JavaScript? This is related with How to obtain anchor part of URL after # in php

Community
  • 1
  • 1
Santanu
  • 7,764
  • 7
  • 26
  • 24

2 Answers2

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];

See also doing substring in window.location.hash

Community
  • 1
  • 1
Gumbo
  • 643,351
  • 109
  • 780
  • 844