url - How to get query string values in JavaScript containing special characters? -


i have followed answer great post: how can query string values in javascript?

however, issue having on occasions, query string values contain special characters, example * + - / etc eg:

?userid=de+8d49b7*8a85a3/222343 

the above function not cater these. how can query string values , inc special characters? have tried:

function getparameterbyname(name) {     name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");     var regex = new regexp("[\\?&]" + name + "=([^&#]*)"),         results = regex.exec(location.search);     return results === null ? "" : decodeuricomponent(results[1].replace(/\+/g, " ")); }  var prodid = encodeuricomponent(getparameterbyname('prodid')); 

the solution must work in ie8+ too.

this should trick:

function getparameterbyname(name,ovr) {     name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");     var regex = new regexp("[\\?&]" + name + "=([^&#]*)"),         results = regex.exec(ovr || location.search);     return results === null ? "" : decodeuricomponent(results[1]); }  var prodid = getparameterbyname('prodid','?prodid=de+8d49b7*8a85a3/222343&userid=23243*--++/231'); console.log('url: ?prodid=?prodid=de+8d49b7*8a85a3/222343&userid=23243*--++/231\nprodid: ' + prodid); 

just remove .replace(/\+/g, " ") returning statement have + characters instead of ""(spaces).

is important note introduce ambiguity values you're getting because urls can't have spaces when encode url have + characters instead of spaces. if you're willing use method must sure values you're retrieving don't have spaces.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -