From the very beginning, here are three <two> three "β three approaches:
Save filter in URL (Good)
Just enter window.location
and dojo/io-query::queryToObject()
string with dojo/io-query::queryToObject()
:
require(['dojo/io-query'], function (ioQuery) { var uri = window.location.href; var query = uri.substring(uri.indexOf("?") + 1, uri.length); query = ioQuery.queryToObject(query); });
( Documentation for dojo / io-query )
Store filter in cookie (better)
The dojo/cookie
module makes this very, very simple:
require(['dojo/cookie', 'dojo/json'], function (cookie, json) { var filter = { ... }; cookie("myFilter", json.stringify(filter));
( Documentation for dojo / cookie )
Obviously, the user must have cookies in order for this to work, but it is much cleaner than storing a bunch of variables in the URL to bookmark them.
Use HTML5 local storage as suggested by Dimitri M : (even better)
Check if the user agent supports local storage, and if so, use this global variable to store the filter:
require(['dojo/json'], function(json) { function supportsLocalStorage() { return ('localStorage' in window) && window['localStorage'] !== null; } var filter = { ... }; if(supportsLocalStorage()) { localStorage.setItem("myFilter", json.stringify(filter)); }
The advantage of using web storage is that you can store much more data than cookies .
Perhaps you can use cookies as a reserve for browsers that do not support local storage (i.e. when supportsLocalStorage()
returns false
) by adding extra overhead to your design, so this is ultimately your call, depending on which browsers you want to support.
Web Storage Browser Compatibility