【问题标题】:Pass javascript variable to url parameter将 javascript 变量传递给 url 参数
【发布时间】:2021-07-28 08:45:15
【问题描述】:
我想传递 javascript 字符串变量并在 URL 上呈现它们。
例如,如果我有 URL =“xyc.com”。一旦填充了一个变量 (a),我希望该变量在地址栏中呈现为 URL = "xyc.com/?a=" .
我目前在这个应用程序的 javascript 中使用 flask 和 .ajax 函数(变量被填充的地方)。有人可以为我提供一些有关如何完成此任务的指导吗?谢谢!
【问题讨论】:
标签:
javascript
python
html
flask
url
【解决方案1】:
如果你想在不重新加载页面的情况下替换 URL
window.history.replaceState(null, null, new_url);
如果您想使用新网址重新加载页面
window.location.href = new_url
【解决方案2】:
试试这个替换网址:
<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试试吧