将javascript变量传递给url参数

时间:2021-05-06 03:05:02

标签: javascript python html flask url

我想传递 javascript 字符串变量并在 URL 上呈现它们。

例如,如果我有 URL = "xyc.com"。一旦填充了变量 (a),我希望在地址栏中呈现该变量,其中 URL = "xyc.com/?a=" .

我目前在此应用程序中使用 javascript(填充变量的位置)中的 flask 和 .ajax 函数。有人可以为我提供一些有关如何完成此操作的指导吗?谢谢!

2 个答案:

答案 0 :(得分:0)

如果您想在不重新加载页面的情况下替换 URL

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

如果您想使用新网址重新加载页面

window.location.href = new_url

答案 1 :(得分:0)

试试这个来替换 URL:

<label>
  End URL<input type='text' onInput='modURL()' id='append'>
<label>
<script>
  let url = location.href;
  localStorage.setItem("url",url);
  function modURL() {
    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>

试试这个fiddle

相关问题