0

You might think this is a duplicate, but it's not. I've seen the other SO answers about changing the browser url with replaceState() but I have a problem...

On one webpage on my website, it receives a lot of parameters, so the link goes on and on and on... I did replaceState(), and it shortened the URL by a lot. That worked. If I go on another link, and I go back in history, the URL will still be the same that I defined on replaceState() and the page gives a 404 error. Is there a way to just load the link the way it is and then temporarily change the URL so the user doesn't see a huge link but the system uses the actual URL?

I'm using JSP, go ahead and give me answers in JavaScript, JQuery, or Java.

Mingle Li
  • 1,322
  • 1
  • 15
  • 39
  • passing variables in post? – Bdloul Aug 21 '15 at 14:21
  • @Bdloul As far as I know, passing parameters in POST will just fill the URL with parameters...and GET will completely ignore all the parameters after the `?`. – Mingle Li Aug 21 '15 at 14:23
  • You could store the parameters in localstorage (ie a cookie) then have your next page un-pick them from localstorage, thus reducing the size needed of the URL in the frst place ? – user2808054 Aug 21 '15 at 14:29
  • PS. a POST request doens't need to add anything to the url- info is passed via the post mechanism. You could issue the url with nothing after the url ? and all info is passed as a POST request. – user2808054 Aug 21 '15 at 14:31
  • @user2808054 how would I store the parameters in a cookie? Can you make an answer for that? – Mingle Li Aug 21 '15 at 14:35
  • You'd basically just take whatever the parameters are that you're replacing out of the url and stick them in encoded form in a cookie or localstorage. Then, the next time you load the page, you check that cookie and if it exists, use those parameters in whatever script was doing something with them as an alternate source of parameter input. If the user hits the page again with some of those key parameters back in the url again, you'd probably want to wipe away the stored parameters so that the url ones take precedence (before you replaceStated them away as well). – Dtipson Aug 21 '15 at 14:38
  • @Dtipson Most of the stuff, I don't understand, cause you know, I'm 2noob! If you could give me an answer (complete with code) that'll be great! – Mingle Li Aug 21 '15 at 14:42
  • @TheJuniorProgrammer done - hope it helps – user2808054 Aug 21 '15 at 14:43

1 Answers1

1

You could store the parameters in localstorage (ie a cookie) then have your next page un-pick them from localstorage, thus reducing the size needed of the URL in the frst place. Example code (stolen from Storing Objects in HTML5 localStorage) :

// add to storage
localStorage.setItem('myAttibute', 'acceptable');

// Retrieve the object from storage (on another page)
var sMyAttibute= localStorage.getItem('myAttibute');

alert("myAttibute=" + sMyAttibute);

Hopefully it'll tell you that my attribute is acceptable.

Community
  • 1
  • 1
user2808054
  • 1,376
  • 1
  • 11
  • 19