7

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

alex
  • 8,904
  • 6
  • 49
  • 75
Mike
  • 3,348
  • 11
  • 45
  • 80
  • @MatthewBlancarte isnt there any shortcut way to get anything after ampersand like what we have for hash tag, if not then what is the approach – Mike Jul 30 '12 at 19:06
  • Have you tried the accepted approach in the link I gave at the bottom of my answer? http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript That will parse the query string. If you were doing this server-side (e.g., PHP/Rails/etc.) it would be much simpler. – Matthew Blancarte Jul 30 '12 at 19:22

3 Answers3

19
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?

Community
  • 1
  • 1
Matthew Blancarte
  • 8,251
  • 2
  • 25
  • 34
  • Is ther a way to get a value after & – Mike Jul 30 '12 at 18:32
  • That's a separate question - please post it as such, or edit the current question. The part after `?` (not `&` is the query string - you asked about the hash). – Mitya Jul 30 '12 at 18:33
  • @Mike it grabs everything after the hash tag, including the hashtag and your query-string without the '?'. :) – Matthew Blancarte Jul 30 '12 at 18:39
  • 1
    Suppose I just have www.domain.com&val=1&val=4, and I need to get &val=1&val=4 – Mike Jul 30 '12 at 18:42
  • what do I need to grab just the query string. – Mike Jul 30 '12 at 18:43
  • `.hash` does not grab the query string – Mitya Jul 30 '12 at 18:43
  • @Utkanos Yes it will, in his case. It will grab all characters following the hash. Try it out. – Matthew Blancarte Jul 30 '12 at 18:45
  • `.hash` does not grab the query string - what the OP has is a contrived query string that is actually part of the hash. A real query string goes before the hash in a URL. – Mitya Jul 30 '12 at 18:46
  • @Mike I hope you don't have a query string with identical keys like val and val. :) Check my updated answer. At the bottom there is a link on how to parse a query string in js. – Matthew Blancarte Jul 30 '12 at 18:46
  • @Utkanos I don't think we are arguing about anything here. I've made it clear that .hash will grab it in this particular case only. :) – Matthew Blancarte Jul 30 '12 at 18:47
  • @MatthewBlancarte - agreed :) Sorry - I just saw your comment where you said "including ... and your query string". – Mitya Jul 30 '12 at 18:48
7

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"
Mitya
  • 33,629
  • 9
  • 60
  • 107
  • This is because you're confusing query strings with hashes. You seem to have a sort of query string-esq string as part of your hash. If you want to extract *that* you'll need some REGEX, string-handling. I'll edit the answer. – Mitya Jul 30 '12 at 18:49
0

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
}
Alex W
  • 37,233
  • 13
  • 109
  • 109