简体   繁体   English

如何使用HTML / Javascript传递URL参数

[英]How to pass a URL parameter in with HTML/Javascript

I have a section on a webapp where the user can search a map for a set of locations and it all works properly but I want the text of the location to show up in the URL so that the user can copy it and share it and it will be the same search. 我在webapp上有一个部分,用户可以在地图上搜索一组位置,并且它们都可以正常运行,但是我希望该位置的文本显示在URL中,以便用户可以复制并共享它以及它。将是相同的搜索。

What is the best way to do this and can I do it with HTML or do I need to use Javascript? 做到这一点的最佳方法是什么?我可以使用HTML还是需要使用Javascript? I am using jinja2 as a templating engine and I am doing the back end in Python but that shouldn't have a big impact on this. 我正在使用jinja2作为模板引擎,并且正在使用Python做后端,但这不会对此产生很大的影响。 Basically I want them to be able to search for "New York City, NYC" and I want it to show up in the URL as something like 基本上,我希望他们能够搜索“ New York City,NYC”,并且希望它在URL中显示为类似

www.url.com/map?location=new%20york%20city%20nyc

The simplest way is just to create a form: 最简单的方法就是创建一个表单:

<form action="/map" method="get">
<label>Search: <input type="search" placeholder="Search" name="location"></label>
<input type="Submit">
</form>

When you enter a search term into the input box and hit "Submit" the browser will make a request to http://www.your-site.com/?q=Your%20Search%20Term - and then you can get the argument from request and do whatever you need to do with it. 当您在输入框中输入搜索词并点击“提交”时,浏览器将向http://www.your-site.com/?q=Your%20Search%20Term发出请求-然后您可以获取参数从request ,做任何你需要做的。

document.location.hash = "some/values/moreValues";

您可以在页面加载时解析哈希值,以在地图上显示所需的内容

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

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