0

I try to get the URL parameter ReturnUrl which contains a hash:

http://localhost/Site?ReturnUrl=%2fPlace#/get

I use the code from How can I get query string values in JavaScript?

but it returns only %2fPlace. Why ?

Community
  • 1
  • 1
Tony
  • 12,405
  • 36
  • 126
  • 226

2 Answers2

2

While location.search + location.hash will return ?ReturnUrl=%2fPlace#/get technically #/get is not part of the ReturnUrl parameter, it is interpreted by the browser as the fragment of the current url.

To handle this more correctly you should be url encoding the # as %23 so the url would be http://localhost/Site?ReturnUrl=%2fPlace%23%2fget

Sam Greenhalgh
  • 5,952
  • 21
  • 37
1

Your problem is in the

"=([^&#]*)" 

regex tag. Remove the hash tag, and add "location.hash" to your regex search and you should be set.

<script type="text/javascript">
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&]*)"),
        results = regex.exec(location.search + location.hash);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

var ReturnUrl = getParameterByName('ReturnUrl');
document.write(ReturnUrl);
</script>
Jonathan Wheeler
  • 2,539
  • 1
  • 19
  • 29