0

I have a url that contains a parameter, which I want to remove. I have created an if...else condition, but I would like to improve it in ES6.

if ( window.location.search.indexOf('&submit')  > -1 ) {
    window.location = urlBase + '/' + window.location.search;
} else {
    window.location = urlBase + '/' + window.location.search + '&submit=1';
}

Thank you in advance.

Kevin Py
  • 2,459
  • 2
  • 22
  • 33

1 Answers1

3

Not sure what your goal is, but here I've simply used includes and template literals.

if (window.location.search.includes('&submit')) {
    window.location = `${urlBase}/${window.location.search}`;
} else {
    window.location = `${urlBase}/${window.location.search}&submit=1`;
}
Randy
  • 4,351
  • 2
  • 25
  • 46