local storage Filter url not working

Experts,
 We have created the custom page in java script. we are displaying data in view. When we click on individual item and save items not display. And that's only when we have "&" symbol in url. We save filter criteria in local storage.

Before Save url :

http://ABC.aspx?#InplviewHash97428e6a-5c1a-4445-a133-336fd3805e7c=FilterField1%3Dfield1-FilterValue1%3DABC%2520%2526%2520Xyz#InplviewHashd1f5ace6-ff21-41ae-a01e-6e088d5b1f42=FilterField1%3Dfield1-FilterValue1%3DABC%2520%2526%2520Xyz
After Save url:

http://abc.aspx?#InplviewHash97428e6a-5c1a-4445-a133-336fd3805e7c=FilterField1=field1-FilterValue1=ABC%20%26%20XYZ#InplviewHashd1f5ace6-ff21-41ae-a01e-6e088d5b1f42=FilterField1=field1-FilterValue1=ABC%20%26%20XYZ

Looks like if we replace "%" to %25% then it should work. Can you please me to fix this issue?
sharepoint0520Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Answer_MeCommented:
you can try encodeuricomponent before save and decodeuricomponent method when retrieving saved value
sharepoint0520Author Commented:
Hi,
 We are using below code to store filtervalue.

//Store filtervalues in session
function getFilterFromUrl() {
   var qs = decodeURIComponent(window.location.href).replace(/[^#]+[\aspx]\s*/, '').contains("#InplviewHash")? decodeURIComponent(window.location.href).replace(/[^#]+[\aspx]\s*/, '') : "";
   localStorage.setItem("agentFilterUrl",qs);
}

Do we need to use "&" symbol in replace command? Do we need to encode? and How?

Thank You
Answer_MeCommented:
when you are storing use encodeuricomponent method and 'no' you don't need to call replace method
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

sharepoint0520Author Commented:
Hi,
  We are calling on click event when we try to open edit form.

return "<a href='/sites/abc.aspx?ID="+ sID +"' onclick='return getFilterFromUrl();'>"+ sName + "</a>";

This problem occur when we have "&" sign in filter url.
sharepoint0520Author Commented:
Hi,
  And when we save the data we use the filter values,

$('#btnSave').click(function()
            {
                  //ExecuteOrDelayUntilScriptLoaded(SaveTerritoryInputs(), "sp.js");
                  SaveInputs();
                  var filterValues = localStorage.getItem("agentFilterUrl");
                  var lvNominationListLink = localStorage.getItem("lvNominationListLink");
                  var link = filterValues!=""?lvNominationListLink+filterValues+"#divAgentNomination":lvNominationListLink+"#divAgentNomination";
                  window.location.href = link;
            });

Please Advice
sharepoint0520Author Commented:
Hi,
 I have found one article. Can you please me to understand and fix my issue?

3.2. The encodeURI() and decodeURI() Functions are Stupid
The second thing to note is the encodeURI() and decodeURI() are idiotic and worthless. I've strained my brain pretty hard trying to think of any application where it would be entirely sensible to use them, and haven't come up with much of anything.
Here's the concept. There are a few characters that clearly are the most important ones to encode, because if they weren't encoded then they could mess up the parsing of the URL. Things like '&' and '=' which could be confused with query string separators, and '#' which could be confused with the start of a location tag.

So what encodeURI() does is encode all sorts of other characters, but NOT those important-to-encode characters. So it encodes all the stuff that isn't really all that critical to encode, but leaves behind the important stuff. The list of characters it does not encode (but which encodeURIComponent() does encode) is:

    # $ & + , / ; = ? @
How could this possibly be useful? Well, if you assemble a whole URL without doing any encoding, and you happen to know somehow that the only instances of those particular characters that appear in it are ones used for their special functions, then you could encode the whole URL at once by calling encodeURI() on the whole thing. I guess that's the theory anyway. It seems clearly more robust just to encode the bits as you assemble them.

The decodeURI() function is the inverse of this. It will decode much of the string, but it will leave things like "%26" unchanged, because that would decode into an ampersand, and you wouldn't want to decode those, now would you?

So these are basically useless and you should forget they exist. The versions you want are the ones with the ridiculously long names, encodeURIComponent() and decodeURIComponent().
leakim971MultitechnicianCommented:
Encode only ONE time :
//Store filtervalues in session
function getFilterFromUrl() {
    var qs = window.location.href;
    if(qs.contains("#InplviewHash"))
        qs = window.location.href.replace(/[^#]+[\aspx]\s*/, '');
    localStorage.setItem("agentFilterUrl", encodeURIComponent(qs));
} 

Open in new window


and of course decode it ONE time :
var filterValues = decodeURIComponent(localStorage.getItem("agentFilterUrl"));

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sharepoint0520Author Commented:
Hi,
  So decode will give us "%" to "%25" ?
leakim971MultitechnicianCommented:
encodedecode/encode
sharepoint0520Author Commented:
Hi,
 Sorry to say but it did not work out after changing code. There are couples of JS file so difficult. I will keep looking the code but if i don't change following code and if i have to replace "%" to "%25" what should i do? It's working for all other filters except some url which has "&".

//Store filtervalues in session
function getFilterFromUrl() {
   var qs = decodeURIComponent(window.location.href).replace(/[^#]+[\aspx]\s*/, '').contains("#InplviewHash")? decodeURIComponent(window.location.href).replace(/[^#]+[\aspx]\s*/, '') : "";
   localStorage.setItem("agentFilterUrl",qs);
}
sharepoint0520Author Commented:
leakim971,
 After implementing your code, when i clicked save the url comes without any filter value. It's remove after #InplviewHash. So there is not filter at all.
leakim971MultitechnicianCommented:
maybe I miss a filter in your logic
I just want to show your main issue is you're using decodeURI too much time
maybe rewrite your code with more than one line

var qs =window.location.href;
qs = qs.replace(....
qs = qs.replace(....
qs = qs.replace(....
qs = qs.replace(....
qs = decodeURIComponent(qs);
localStorage.setItem("agentFilterUrl",qs); 

Open in new window

sharepoint0520Author Commented:
leakim971,
 Your code is working. There was a problem with function name where we store filter criteria in session. Thank you so much.
leakim971MultitechnicianCommented:
you're welcome
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.