How to get multiple values from jquery hash
-
19-06-2021 - |
题
How can i get multiple values in a jquery hash?
Example:
http://mysite.com/#l:value&v:id1212 same as but without page refresh http://mysite.com/?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?
解决方案
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);
不隶属于 StackOverflow