简体   繁体   中英

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.

What is the best way to do this and can I do it with HTML or do I need to use 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. 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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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