How to get hashtag value and ampersand value of url in javascript?

I have url like http://www.example.com/folder/file.html#val=90&type="test"&set="none"&value="reset?setvalue=1&setvalue=45"

Now I need to get part of the URL from the beginning of C #. How do I get this, I tried to use it window.location.search.substr();, but it looks like it is looking? in the url. is there any way to get url value after #

How to get part of the url from ampersand and

Thanks Michael

+5
source share
3 answers
var hash = window.location.hash;

Further information here: https://developer.mozilla.org/en/DOM/window.location

Update: this will capture all characters after the hashtag, including any query strings. From the MOZ manual:

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.

, PARSE , , , , : JavaScript?

+13

:

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

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

[EDIT - , , query-string-esq, , /.

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"
+5

We get the values #and &:

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
source

All Articles