From year to year, I face the same problem. How to add, change or delete a parameter to a certain address in a string form. This can be done quickly and dirty with, for example, regular expressions or to find some kind of ready-made solution. Often, it may also be necessary, for example, to change the path in the address or change the protocol from HTTP to HTTPS, etc.
In general, I want to do this simply and clearly. In this case, you want a reasonable compromise. I have met some libraries that provide powerful functionality, but at the same time in terms of volume - tens of kilobytes of JavaScript code. A few tens of kilobytes, for example, to replace the parameter in the QueryString? Eh ...
In general, I wrote my solution, which I found convenient, first of all, for myself. When solving, he also borrowed the accumulated experience of comrade Jan Wolter regarding
parsing a QueryString in JavaScript (caution is English). At least this somewhat increased the amount of code, but it is able to get rid of some problems.
')
So my solution can be taken from here -
github.com/Mikhus/jsurl . The license is MIT, so do whatever you want. The minified and gzip-compressed code turned out to be less than a kilobyte, which should be acceptable. Dependencies on any other libraries are not observed. The API is simple: first we turn the string with the address into an Url object and then treat it depending on the situation, either as a string or as an object. For example:
var u = new Url('http://user:pass@example.com:8080/some/path?foo=bar#anchor');
Any comments, improvements, found bugs, forks and other kosher things are welcome and thankful.