[英]Removing elements from title and url of page with jQuery
I have a Google Instant style search script written in jQuery which pulls results from a PHP script.我有一个用 jQuery 编写的 Google Instant 样式搜索脚本,它从 PHP 脚本中提取结果。 When a user queries something, their query is displayed in the title of the page as " QUERY - My Search Script" and in the url of the page as # QUERY .
当用户查询某些内容时,他们的查询在页面标题中显示为“ QUERY - My Search Script”,在页面的 url 中显示为 # QUERY 。 However, when you delete all the text from the search box the # still stays in the url and the title says " - My Search Script".
但是,当您从搜索框中删除所有文本时, #仍保留在 url 中,并且标题显示“-我的搜索脚本”。 How can I make it so when all of the search box content is cleared, the script removed the title and the #?
当清除所有搜索框内容时,脚本如何删除标题和#?
My jQuery code is:我的 jQuery 代码是:
$(document).ready(function(){
$("#search").keyup(function(){
var search=$(this).val();
var query=encodeURIComponent(search);
var yt_url='search.php?q='+query+'&category=web';
window.location.hash=query;
document.title=$(this).val()+" - My Search Script";
$.ajax({
type:"GET",
url:yt_url,
dataType:"html",
success:function(response){
$("#result").html(response);
}
});
});
});
You can tidy the <title>
up easy enough but you won't be able to consistently remove the #
from the URL once you've set a hash.您可以轻松地整理
<title>
,但是一旦设置了 hash,您将无法始终从 URL 中删除#
。 The only browser that seems to get rid of it is Firefox.唯一似乎摆脱它的浏览器是 Firefox。
$(document).ready(function(){
$("#search").keyup(function(){
var search=$(this).val();
var query=encodeURIComponent(search);
var yt_url='search.php?q='+query+'&category=web';
window.location.hash=query;
// You may want to use a better title than '\xa0' (blank)
document.title = search=='' ? '\xa0' : search+" - My Search Script";
$.ajax({
type:"GET",
url:yt_url,
dataType:"html",
success:function(response){
$("#result").html(response);
}
});
});
});
How about something like this:像这样的东西怎么样:
$(document).ready(function(){
$("#search").keyup(function(){
var search=$(this).val();
var query=encodeURIComponent(search);
var yt_url='search.php?q='+query+'&category=web';
$.ajax({
type:"GET",
url:yt_url,
dataType:"html",
success:function(response){
$("#result").html(response);
if (query == "") {
window.location.hash=query;
document.title=$(this).val()+" - My Search Script";
} else {
window.location.hash=query;
document.title="My Search Script";
}
}
});
});
});
The changes I made are:我所做的更改是:
Try this:尝试这个:
$(document).ready(function() {
$('#search').keyup(function() {
var search = $(this).val();
if(search == '') {
window.location.hash = '';
document.title = 'My Search Script';
$('#result').empty();
}
else {
window.location.hash = encodeURIComponent(search);
document.title = search + ' - My Search Script';
$.ajax({
type: 'GET',
url: 'search.php',
data: {q: search, category: 'web'},
dataType: 'html',
success: function(response){
$('#result').html(response);
}
});
}
});
});
I've used the data
option of $.ajax
so that you don't need to build the url yourself.我使用了
$.ajax
的data
选项,这样您就不需要自己构建 url。
I don't think you can guarantee to get rid of the hash in the URL: Some browsers remove it, others don't.我认为您不能保证在 URL 中删除 hash:有些浏览器将其删除,有些则不会。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.