local storage Filter url not working

sharepoint0520
sharepoint0520 used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
you can try encodeuricomponent before save and decodeuricomponent method when retrieving saved value

Author

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
when you are storing use encodeuricomponent method and 'no' you don't need to call replace method
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

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.

Author

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

Author

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().
Multitechnician
Top Expert 2014
Commented:
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

Author

Commented:
Hi,
  So decode will give us "%" to "%25" ?
leakim971Multitechnician
Top Expert 2014

Commented:
encodedecode/encode

Author

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);
}

Author

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.
leakim971Multitechnician
Top Expert 2014

Commented:
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

Author

Commented:
leakim971,
 Your code is working. There was a problem with function name where we store filter criteria in session. Thank you so much.
leakim971Multitechnician
Top Expert 2014

Commented:
you're welcome

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial