Skip to content Skip to sidebar Skip to footer

Pass Javascript Variable To Url Parameter

I want to pass javascript string variables and render them on the URL. For example, if I have URL = 'xyc.com'. Once there is a variable (a) populated, I want that variable to be re

Solution 1:

If you want to replace the URL without reloading the page

window.history.replaceState(null, null, new_url);

If you want to reload the page with your new URL

window.location.href = new_url

Solution 2:

Try this for replacing the URL:

<label>
  End URL<inputtype='text'onInput='modURL()'id='append'><label><script>let url = location.href;
  localStorage.setItem("url",url);
  functionmodURL() {
    if (typeof (history.pushState) != "undefined") {

      let newURL = localStorage.getItem("url")+'?q='+document.getElementById('append').value;
      let title = document.getElementById('append').value;
      let obj = { Title: title, Url: newURL };

      console.log("url ", newURL);
      history.pushState(obj, obj.Title, obj.Url);
    } else {
      alert("Browser does not support HTML5.");
    }
  }
</script>

Try it in this fiddle

Post a Comment for "Pass Javascript Variable To Url Parameter"