2012-07-30 15 views
5

Ho un url del tipo http://www.example.com/folder/file.html#val=90&type="test"&set="none"&value="reset?setvalue=1&setvalue=45"Come ottenere il valore hashtag e il valore di e commerciale di un URL in Javascript?

Ora ho bisogno di ottenere la parte di URL a partire dal #, Come faccio a ottenere che, ho provato ad utilizzare window.location.search.substr(); ma sembra che che cerca? in un url. c'è un metodo per ottenere il valore di URL dopo #

Come posso anche ottenere una parte di URL da ampersand &

Grazie, Michael

+0

Qualsiasi fortuna @ Mike? –

+0

@MatthewBlancarte non c'è un modo rapido per ottenere qualcosa dopo la e commerciale come quello che abbiamo per il tag hash, se no allora qual è l'approccio – Mike

+0

Hai provato l'approccio accettato nel link che ho dato in fondo alla mia risposta? http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript Questo analizzerà la stringa di query. Se steste facendo questo lato server (ad es. PHP/Rails/etc.) Sarebbe molto più semplice. –

risposta

13
var hash = window.location.hash; 

Maggiori informazioni qui: https://developer.mozilla.org/en/DOM/window.location

Aggiornamento: questo prenderà tutti i caratteri dopo l'hashtag, comprese le stringhe di query. Dal manuale MOZ:

window.location.hash === the part of the URL that follows the # symbol, including the # symbol. 
You can listen for the hashchange event to get notified of changes to the hash in 
supporting browsers. 

Ora, se è necessario analizzare la stringa di query, che credo si fa, check this out qui: How can I get query string values in JavaScript?

+0

C'è un modo per ottenere un valore dopo & – Mike

+0

Questa è una domanda a parte - per favore pubblicala come tale o modifica la domanda corrente. La parte dopo '?' (Non '&' è la stringa della query - hai chiesto dell'hash). – Utkanos

+0

@Utkanos Ho modificato ora per favore rispondi se sai – Mike

0

In questo modo ottenere i valori # e &:

var page_url = window.location + "";  // Get window location and convert to string by adding "" 
var hash_value = page_url.match("#(.*)"); // Regular expression to match anything in the URL that follows # 
var amps;         // Create variable amps to hold ampersand array 

if(hash_value)        // Check whether the search succeeded in finding something after the # 
{ 
    amps = (hash_value[1]).split("&");  // Split string into array using "&" as delimiter 
    alert(amps);       // Alert array which will contain value after # at index 0, and values after each & as subsequent indices 
} 
+0

non può leggere la proprietà '1' di null è l'errore che ottengo quando ho eseguito questo in console – Mike

+1

Questo sarà errore in qualsiasi momento non v'è alcun hash perché una partita si presume, non controllato per. – Utkanos

+0

Si può spiegare la vostra seconda riga di codice – Mike

5

per afferrare l'hash:

location.hash.substr(1); //substr removes the leading # 

Per g rab la stringa di query

location.search.substr(1); //substr removes the leading ? 

[EDIT - dal momento che sembrano avere una stringa sorta di query-string-Esq che è in realtà parte del vostro hash, il seguente sarà recuperare e analizzare in un oggetto di nome/abbinamenti valore .

var params_tmp = location.hash.substr(1).split('&'), 
    params = {}; 
params_tmp.forEach(function(val) { 
    var splitter = val.split('='); 
    params[splitter[0]] = splitter[1]; 
}); 
console.log(params.set); //"none" 
+0

location.search.substr (1); non sembra funzionare per me. – Mike

+0

Questo perché confondi le stringhe di query con gli hash. Sembra che tu abbia una sorta di stringa stringa-esq di query come parte del tuo hash. Se vuoi estrarre * that * avrai bisogno di qualche REGEX, gestione delle stringhe. Modificherò la risposta. – Utkanos

Problemi correlati