1

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?

2 Answers 2

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];
Sign up to request clarification or add additional context in comments.

Comments

3

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

1 Comment

Thanks for the reply, but the method of Engineer is exactly what i was looking for.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.