简体   繁体   English

将 javascript 变量传递给 url 参数

[英]Pass javascript variable to url parameter

I want to pass javascript string variables and render them on the URL.我想传递 javascript 字符串变量并将它们呈现在 URL 上。

For example, if I have URL = "xyc.com".例如,如果我有 URL = "xyc.com"。 Once there is a variable (a) populated, I want that variable to be rendered in the address bar with URL = "xyc.com/?a=".一旦填充了变量 (a),我希望使用 URL = "xyc.com/?a=" 在地址栏中呈现该变量。

I am currently using the flask and.ajax function in javascript (where the variable is being populated) in this application.我目前正在使用 flask 和.ajax function 在 ZDE9B9ED78D78D7E2E1DCEEFFEE78 中使用 ZDE9B9ED78D78D7E2E1DCEEFFEE78。 Can someone please provide me some guidance on how I can accomplish this?有人可以为我提供一些有关如何完成此任务的指导吗? Thank you!谢谢!

If you want to replace the URL without reloading the page如果要在不重新加载页面的情况下更换 URL

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

If you want to reload the page with your new URL如果您想使用新的 URL 重新加载页面

window.location.href = new_url

Try this for replacing the URL:试试这个替换 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>

Try it in this fiddle试试这个小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM