Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force browser not to convert 'comma' to 2C symbols using JS?

Tags:

javascript

I'm changing current user's path through a function:

function setSomeValue(someValues) {
      var query = '';
      for (var i = 0; i < someValues.length; i++) {
        query += someValues[i] + ',';
      }
      if ('URLSearchParams' in window) {
        var searchParams = new URLSearchParams(window.location.search);
        searchParams.set("paramName", query);

        var newRelativePathQuery = window.location.pathname + '?' + searchParams.toString();
        history.pushState(null, '', newRelativePathQuery);
      }
    }

As you can see, I'm adding to user's location new words and want new location to be like this:

www.site.com?paramName=value1,value2,

But browser converts my commas into %2C so I get this:

www.site.com?paramName=value1%2Cvalue2%2C

What should be done to make pushing commas to URL possible?


like image 451
Lab Lab Avatar asked Oct 16 '25 10:10

Lab Lab


1 Answers

(copy & paste from several comments)

It might be due to URLSearchParams and its toString method implementation - but we can’t know, because you have not shown us what that actually is. If that is not deliberately encoding the comma, and the browser simply does it automatically - then there’s little you can do about that.

If newRelativePathQuery contains the encoded versions already, maybe they could be replaced back to normal commas. But if history.pushState does it, then “other ways” to create the URL itself won’t help you much.

Since a debug output showed that newRelativePathQuery contains the encoded commas already, you can try and replace them back to commas, and see if that “survives” being pushed to the history then.

like image 175
CBroe Avatar answered Oct 19 '25 01:10

CBroe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!