Consider this line: #page?param1=a¶m2=b¶m3=c
The hybrid application that I worked on uses window.location.hashto route the application to the desired page. Often these URLs contain parameters after the hash. Of course, this is not a standard, but it is a good solution that is perfect for our application.
I need to create a function that will take all the parameters in the hash and returns them to the object, for example {param: value}.
I tried solving other issues that include window.location.search, but unfortunately, it just returns an empty string when the parameters are after the hash.
My attempt looks like this:
return JSON.parse('{"' + decodeURI(window.location.hash).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g,'":"') + '"}');
The solution is taken from another question that uses window.location.search, but the use window.location.hashdoes not work correctly, the first parameter (after the question mark) is displayed as undefined.
How to create a function that returns hash parameters in an object?
The desired result for the line above will be as follows:
{ param1: 'a', param2: 'b', param3: 'c' }
source
share