Skip to content

Commit

Permalink
Add uri decode when return params.
Browse files Browse the repository at this point in the history
  • Loading branch information
websanova committed Jul 26, 2015
1 parent 764ed8d commit 71f054c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
3 changes: 3 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ test('query string', function() {
deepEqual( window.url( '?poo', 'http://domain.com?poo' ), '' );
deepEqual( window.url( '?poo', 'http://domain.com?' ), null );
deepEqual( window.url( '?poo', 'http://domain.com' ), null );

deepEqual( window.url( '?poo', 'http://domain.com?poo=a+b' ), 'a b' );
deepEqual( window.url( '?poo', 'http://domain.com?poo=javascript%20decode%20uri%20%2B%20sign%20to%20space' ), 'javascript decode uri + sign to space' );
});

test('url fragment', function() {
Expand Down
8 changes: 6 additions & 2 deletions url.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ window.url = (function() {
function isNumeric(arg) {
return !isNaN(parseFloat(arg)) && isFinite(arg);
}

function decode(str) {
return decodeURIComponent(str.replace(/\+/g, ' '));
}

return function(arg, url) {
var _ls = url || window.location.toString();
Expand Down Expand Up @@ -56,15 +60,15 @@ window.url = (function() {
if(arg.charAt(0) === '?') { params = (params.split('?')[1] || '').split('#')[0]; }
else if(arg.charAt(0) === '#') { params = (params.split('#')[1] || ''); }

if(!arg.charAt(1)) { return params; }
if(!arg.charAt(1)) { return (params ? decode(params) : params); }

arg = arg.substring(1);
params = params.split('&');

for(var i=0,ii=params.length; i<ii; i++)
{
param = params[i].split('=');
if(param[0] === arg) { return param[1] || ''; }
if(param[0] === arg) { return (param[1] ? decode(param[1]) : param[1]) || ''; }
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion url.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 71f054c

Please sign in to comment.