I have a 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 the portion of url from starting from #, How do I get that, I tried using window.location.search.substr();
but looks like that searches for ? in a url. is there a method to get the value of url after #
How do I also get a portion of url from ampersand &
Thanks,
Michael
var hash = window.location.hash;
More info here: https://developer.mozilla.org/en/DOM/window.location
Update: This will grab 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.
Now, if you need to PARSE the query string, which I believe you do, check this out here: How can I get query string values in JavaScript?
To grab the hash:
location.hash.substr(1); //substr removes the leading #
To grab the query string
location.search.substr(1); //substr removes the leading ?
[EDIT - since you seem to have a sort query-string-esq string which is actually part of your hash, the following will retrieve and parse it into an object of name/value pairings.
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"
This will get the #
and &
values:
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
}