I’m decoding my URL parameters in .NET as explained in this article.
In some cases I need to get the URL parameters in Javascript. But the problem is that some parameter values end at a ‘=’.
Example: ?itemID=Jj1TI9KmB74=&cat=1
Javascript function:
function getUrlVars() {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;}
I know my problem is at the split-function in the for-loop but I don’t know how to solve it.
I also used jsuri for this but the problem still exists.
Can I solve this in Javascript or do I need to reconsider my encryption-method?
Having
=unencoded in the URI query is invalid. You should escape it. However, for completeness, if you really needed to do this, try this:Extra note: if you escaped the
=using URI encoding, on the server side (e.g. $_GET) it’ll be automatically decoded. With JavaScript andlocation, however, you must decode it first (decodeURIComponent) to work with it.Instead of doing:
where subsequent equals signs are split too, do this instead (a non-greedy match before the first equals symbol for the key name, the rest for the value):