How to replace or remove specific string from url in jquery. - Magesan

How to replace or remove specific string from url in jquery.

How to replace or remove specific string from url in jquery.

Replacing some specific string from url is somewhat tricky job we need to do in our day to day life in IT field for making something dynamic.

Here is the working solution you can try given below.

const urlParams = new URLSearchParams(window.location.search); // need this for search in url
var param = ""; // declared param variable
var dynamicHref = window.location.href; // dynamic link of current url
param_x = urlParams.get("string=55,22"); // find string from url
param_x = param_x.split(',').join('%2C'); // replace comma with (& or %2C)
param_x = dynamicHref.replace("string=" + param_x, ""); // replace string with null
$(".custom-href-class").attr("href", param_x); // you can now put dynamic link to anchor tag.

Above given solution is tried and found working. If you are stuck somewhere then feel free to comment down. Hopefully i try to gives you the solution.

Leave a Reply

Your email address will not be published. Required fields are marked *.

*
*
You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>