Domanda

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?

È stato utile?

Soluzione

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];

Altri suggerimenti

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top