Determine the difference between & and% 26 in location.hash

Analyzing location.hash with this simple javascript code:

<script type="text/javascript">alert(location.hash);</script>

It's hard for me to separate GET variables containing a and (encoded as% 26) and a and used to separate the variables.

Example one:

  • code = php & age = 15d

Example two:

  • Code = PHP% 20% 26% 20code & Age = 15d

As you can see, example 1 has no problems, but getting javascript in order to know that "code = php and code" in example two is beyond my capabilities:

(Note: I do not use these variable names, and changing them to something else will only work until the search query matches the search key, so I would not consider this a valid solution.)

+3
4

% 26 (). "& ; (" ") URI. Escaping & " % 26 " , " a "" % 61".

, . , "+ XX hex-encoding" :

hxxp://www.example.com/page#code=php+20+2B+20php&age=15d

function encodeHashComponent(x) {
    return encodeURIComponent(x).split('%').join('+');
}
function decodeHashComponent(x) {
    return decodeURIComponent(x.split('+').join('%'));
}

function getHashParameters() {
    var parts= location.hash.substring(1).split('&');
    var pars= {};
    for (var i= parts.length; i-->0;) {
        var kv= parts[i].split('=');
        var k= kv[0];
        var v= kv.slice(1).join('=');
        pars[decodeHashComponent(k)]= decodeHashComponent(v);
    }
    return pars;
}
+4

Firefox 3.1 , location.hash, JavaScript , .

, -, .

, (JS 1.6 ):

function pairs(xs) {
  return xs.length > 1 ? [[xs[0], xs[1]]].concat(pairs(xs.slice(2))) : []
}

function union(xss) {
  return xss.length == 0 ? [] : xss[0].concat(union(xss.slice(1)));
}

function splitOnLast(s, sub) {
  return s.indexOf(sub) == -1 ? [s] : 
                                [s.substr(0, s.lastIndexOf(sub)),
                                 s.substr(s.lastIndexOf(sub) + sub.length)];
}

function objFromPairs(ps) {
  var o = {};
  for (var i = 0; i < ps.length; i++) {
    o[ps[i][0]] = ps[i][1];
  }
  return o;
}

function parseHash(hash) {
  return objFromPairs(
           pairs(
             union(
               location.hash
                       .substr(1)
                       .split("=")
                       .map(
                         function (s) splitOnLast(s, '&')))))
}

>>> location.hash
"#code=php & code&age=15d"
>>> parseHash(location.hash)
{ "code": "php & code", "age": "15d" }
+1

, , , , unescape (), % 26 %20 .

Edit:

, , decodeURIComponent(), , -: unescape ( ), , .

0

:

var hash = [];
if (location.hash) {
 hash = location.href.split('#')[1].split('&');
}
0

Source: https://habr.com/ru/post/1704340/


All Articles