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 ?

1
  • Can you post your current code? Commented Dec 10, 2014 at 17:04

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.