admin管理员组

文章数量:1295236

How can i get multiple values in a jquery hash?

Example:

:value&v:id1212 same as but without page refresh /?l=value&v=id1212

I thought something like:

var hash = location.hash.replace(/^.*?#/, '');

var lVal = (/^l:/.test( hash ));
var vVal = (/^v:/.test( hash ));

but how can i fix that var lVal only reads untill the & sign? Am i on the right direction?

How can i get multiple values in a jquery hash?

Example:

http://mysite./#l:value&v:id1212 same as but without page refresh http://mysite./?l=value&v=id1212

I thought something like:

var hash = location.hash.replace(/^.*?#/, '');

var lVal = (/^l:/.test( hash ));
var vVal = (/^v:/.test( hash ));

but how can i fix that var lVal only reads untill the & sign? Am i on the right direction?

Share Improve this question edited Jul 8, 2012 at 17:26 rodneyrehm 13.6k1 gold badge42 silver badges56 bronze badges asked Jul 8, 2012 at 17:11 KipKip 5134 silver badges14 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Try to split your string by & sign and then retrieve values:

var hash = location.hash.replace(/^.*?#/, '');
var pairs = hash.split('&');
var lVal = pairs[0].split(':')[1];
var vVal = pairs[1].split(':')[1];

I believe you might be looking for something like History.js to help you ease the pain. Also URI.js has a fragment-abuse plugin for data fragments. Otherwise try something like

var data = {};
var tokens = location.hash.substring(1).split('&');
for (var i=0, l=tokens.length; i < l; i++) {
  var token = tokens[i].split(':');
  // maybe token[1] is escaped or something?!
  data[token[0]] = token[1];
}
console.log(data);

本文标签: javascriptHow to get multiple values from jquery hashStack Overflow