2012-08-12 16 views

Respuesta

35
function getHashValue(key) { 
    var matches = location.hash.match(new RegExp(key+'=([^&]*)')); 
    return matches ? matches[1] : null; 
} 

// usage 
var hash = getHashValue('hash'); 
+1

This res ejecuta todo después de 'hash =' –

+0

@AkshatMittal Edited. – xdazz

+0

¡Esto funciona ahora! –

3

¿Qué tal

location.hash.split('hash=')[1].split('&')[0] 

Esto dividirá el hash en hash= y tomar el valor después de hash = y antes de cualquier otro argumento.

+0

Esta tal cual no funciona, por lo que es 'ubicación .href.split ('hash =') [1] .split ('&') [0] 'funciona –

+0

Necesita probar más, Voto ascendente ahora –

1

Split, en & y luego en =:

pairs = location.hash.substr(1).split('&').map(function(pair) { 
    var kv = pair.split('=', 2); 
    return [decodeURIComponent(kv[0]), kv.length === 2 ? decodeURIComponent(kv[1]) : null]; 
}) 

Aquí pairs habrá una matriz de matrices con la llave en 0 y el valor al 1:

[["hfh","fdg"],["hash","value2x"]] 
2

Si están haciendo extensas manipulaciones de url, entonces deberías verificar JQuery URL plugin.

Para acceder a los parametros en el URL hashes

$.url('http://www.google.com/#hfh=fdg&hash=value2x').fparam('hash'); 

o si su URL actual

$.url().fparam('hash'); 

creo que sirve

+0

Esto ayuda, lo obtuve antes también , Disculpe que no especifique antes, pero no quiero usar jQuery. –

2
location.parseHash = function(){ 
    var hash = (this.hash ||'').replace(/^#/,'').split('&'), 
     parsed = {}; 

    for(var i =0,el;i<hash.length; i++){ 
     el=hash[i].split('=') 
     parsed[el[0]] = el[1]; 
    } 
    return parsed; 
}; 

var obj= location.parseHash(); 
    obj.hash; //fdg 
    obj.hfh; //value2x 
Cuestiones relacionadas