[英]jQuery / JavaScript: Changing URL hash from <input> tag or on form submit
is it possible to somehow affect the hash of the URL from the <input>
tag? 是否有可能以某种方式影响<input>
标记中URL的哈希?
I need to initiate a search without a page refresh, but also want to change the URL hash, for example: search.html#query=hello%20world
, when I have typed hello world
in particular <input>
and hit Enter (or submit). 我需要在不刷新页面的情况下启动搜索,但是还想更改URL哈希,例如: search.html#query=hello%20world
,当我特别<input>
hello world
<input>
并按下Enter键(或提交)时)。
UPD: I have an AJAX search, but I want to keep the history back and forward buttons working. UPD:我有一个AJAX搜索,但是我想保持历史记录的前进和后退按钮有效。
So, each time the user searches something, I want him to stay at the same page, load search results via AJAX and change the page's URL hash to enable the history buttons. 因此,每次用户搜索内容时,我希望他停留在同一页面上,通过AJAX加载搜索结果,并更改页面的URL哈希以启用历史记录按钮。
Use window.location.hash
property to get and set the hash. 使用window.location.hash
属性获取并设置哈希。
var e = document.getElementById('search');
window.location.hash = "#"+escape(e.value);
Yes. 是。
document.getElementById('search-form').onsubmit = function() {
var hash = 'query=' +
encodeURIComponent(document.getElementById('query').value);
window.location.hash = hash;
};
Since you're doing this asynchronously, I can't tell you exactly how to do it, but I'll assume you have some sort of search function in which you get the value of the input
and perform the search: 由于您是异步进行的,因此我无法确切告诉您如何执行此操作,但是我假设您具有某种搜索功能,可以在其中获取input
值并执行搜索:
function your_search_function()
{
var searchString = document.getElementById('your-input').val // get the value
// do your search stuff...
window.location.hash = 'query=' + encodeURIComponent(searchString);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.