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 ?
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 ?
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
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>