0

i have any issue where i want to remove url parameters when on click the div , while some parameters are in words and some are numbers that containing %20 , i am not able to remove those ones , can you please help me .

my url is https://www.schooleducation.com/onlin/courses/?filter=8%27%20-%2015%27

i want to remove 8' - 15' url parameter value from the url when onlclick here is my code

 $('.fileter-container').once('filter-click').on('click', function () {
    var key = $(this).data('key');
    $(this).remove();
    var url = 'https://www.schooleducation.com/onlin/courses/?filter=8%27%20-%2015%27'
    var re = new RegExp("&filter=\\d+");
    var newurl= url.replace(key, '');
    // var newurl = url.split('?')[0];
    location.replace(newurl);
  });
vyshnavi
  • 167
  • 1
  • 3
  • 16
  • 1
    Try decoding URL `decodeURIComponent('https://www.schooleducation.com/onlin/courses/?filter=8%27%20-%2015%27') ` – murli2308 Apr 11 '21 at 15:21
  • 1
    Simpler approach is use `URL API` ... `const newUrl = new URL(url); newUrl.searchParams.delete('filter'); console.log(newUrl.href)` – charlietfl Apr 11 '21 at 15:36

1 Answers1

0

Try decoding the URL string before manipulating it.

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent

Stephen
  • 2,410
  • 3
  • 33
  • 57