Consider this string: #page?param1=a¶m2=b¶m3=c
A hybrid application I have been working on uses window.location.hash to route the application to the right page. Often, these URLs contain parameters after the hash. Sure, this isn't standard, but it's a good solution that works nicely for our application.
I need to create a function that will take all of the parameters in the hash and return them in a object, for example: {param: value}.
I have tried other questions solution's that involve window.location.search but sadly that just returns an empty string when the parameters are after a 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 using window.location.hash doesn't quite work properly, the first parameter (after the question mark) shows as undefined.
How can I create a function that would return hash parameters in an object?
The desired result for the string above would be this:
{ param1: 'a', param2: 'b', param3: 'c' }