I'm facing issues with splitting and parsing window.location.hash correctly.
First of all, we get few parameters in hash, ex:
#loc=austria&mr=1&min=10&max=89
As you surely see it's been created for search. When user clicks on pagination link page is being reloaded with the hash. So far so good.
I created function initialise() that is calling every time when there's hash in the URL:
if (window.location.hash) { var params = (window.location.hash.substr(1)).split("&"); for (i = 0; i < params.length; i++) { var a = params[i].split("="); // Now every parameter from the hash is beind handled this way if (a[0] == "loc") { locationList(a[1]); } } }
Everythig is almost working... When I choose all search params hash is being... cut. For unknown reason for me. I tried to use if( params.indexOf('loc') )
instead of a[0] == "loc"
without any luck.
Could you lend me a hand?
Edit
Of course, I was using var a = ... in the loop, it was only copy-paste error.
The hash property of the Location interface returns a string containing a '#' followed by the fragment identifier of the URL — the ID on the page that the URL is trying to target. The fragment is not percent-decoded. If the URL does not have a fragment identifier, this property contains an empty string, "" .
The Location Hash property in HTML is used to return the anchor part of a URL. It can also be used to set the anchor part of the URL. It returns the string which represents the anchor part of a URL including the hash '#' sign. Syntax: It returns the hash property.
In a URL, a hash mark, number sign, or pound sign ( # ) points a browser to a specific spot in a page or website. It is used to separate the URI of an object from a fragment identifier. When you use a URL with a # , it doesn't always go to the correct part of the page or website.
The location. hash property sets or returns the anchor part of a URL, including the hash sign (#).
You don't need a loop, if it's only the value of loc
from the hash you're after. This should also work.
var lochash = location.hash.substr(1), mylocation = lochash.substr(lochash.search(/(?<=^|&)loc=/)) .split('&')[0] .split('=')[1]; if (mylocation) { locationList(myLocation); }
Concerning the trunctating of the hash after a page reload: imho that isn't related to your loop.
Edit A more modern and more accurate approach:
const result = document.querySelector("#result"); const hash2Obj = "loc=austria&mr=1&min=10&max=89" .split("&") .map(v => v.split("=")) .reduce( (pre, [key, value]) => ({ ...pre, [key]: value }), {} ); result.textContent += `loc => ${hash2Obj.loc} ---- *hash2Obj (stringified): ${JSON.stringify(hash2Obj, null, ' ')}`;
<pre id="result"></pre>
This should be a rather simpler way to read from location.hash:
var hash = window.location.hash.substring(1); var params = {} hash.split('&').map(hk => { let temp = hk.split('='); params[temp[0]] = temp[1] }); console.log(params); //Here are the params to use
and then, you could use
params.access_token //access_token params.id //id
and other params that are available inside the hash
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With