0

I need to change a url parameter after reloading the page. When I refresh the page, need to change that parameter's value.

window.location.href = myUser + '?myuserid=' + myuserid + '&product=' + product + '&domain=' + domain + '&isPassed=' + true;

After reloading page, I have to change the value of isPassed to false.

Hope your support to solve my problem. Thank you

Dulani Maheshi
  • 1,070
  • 1
  • 10
  • 30
  • Would you please clarify what you mean by "I have to change the value of `isPassed`"? This could easily mean that you want to change the value *then reload the page* (again) OR that you're trying to change the value of a variable in the current instance – Tibrogargan Apr 08 '22 at 02:20
  • I redirect to the page then fresh that page. At that moment, I want to change the url parameter's value. – Dulani Maheshi Apr 08 '22 at 02:37
  • So you're really just asking how to change the list of `&` separated `key=value` pairs that come after the `?` in `window.location.href` – Tibrogargan Apr 08 '22 at 02:44
  • yes @Tibrogargan – Dulani Maheshi Apr 08 '22 at 02:48
  • 1
    You could just brute force it with `if (window.location.href.indexOf('isPassed=true') != -1) { window.location.href = window.location.href.replace('isPassed=true', 'isPassed=false') }`. This however is not a good way to do it, as there's a bunch of edge cases that can break it. – Tibrogargan Apr 08 '22 at 03:03

1 Answers1

0

This can be done by modifying the document.location.search. Urls are broken down into protocol (Http, https), hostname (www.stackoverflow.com), pathname (/questions/71790720/how-do-i-change-the-url-parameters-after-reloading-the-page), search (?param1="something"&param2=...), and hash (#something). All of these are accessible with window.location.

const user = "Eric"
const userId = 1
const product = "banana"
const domain = "something"
const isPassed = false
window.location.search = `${user}? 
myuserid=${userId}&product=${product}&domain=${domain}&isPassed=${isPassed}`
Eric Webb
  • 361
  • 3
  • 10