【发布时间】:2012-02-19 04:24:56
【问题描述】:
我在我的网站上使用 Google Site Search,它在每个浏览器中都能正常工作。但我想使用我自己的输入元素,这样我就可以在我的顶部导航中放置一个自定义搜索栏。我找到了一个允许我这样做的脚本,它通过将“?q =”附加到 URL 来工作。搜索登陆页面上的脚本接受查询并将其注入新的 Google 搜索字段。 URL 最终看起来像这样:
www.website.com/search/?q=query
这适用于除 Android 移动设备之外的所有浏览器。
这是我顶部导航中自定义搜索栏的脚本:
<form onsubmit="return submitQuery()" id="search-form">
<input autocomplete="off" id="search" type="text" name="q" maxlength="100">
<button type="submit" id="searchSubmit">Search</button>
</form>
<script>
function submitQuery() {
if ($('#search').val()=='Search') {
alert('Please enter a search term');
return false;
}
else {
window.location = '/search/?q='
+ encodeURIComponent(
document.getElementById('search').value);
return false;
}
}
</script>
这是我的搜索页面上的脚本
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
google.load('search', '1', {language : 'en', style : google.loader.themes.MINIMALIST});
/**
* Extracts the users query from the URL.
*/
function getQuery() {
var url = '' + window.location;
var queryStart = url.indexOf('?') + 1;
if (queryStart > 0) {
var parts = url.substr(queryStart).split('&');
for (var i = 0; i < parts.length; i++) {
if (parts[i].length > 2 && parts[i].substr(0, 2) == 'q=') {
return decodeURIComponent(
parts[i].split('=')[1].replace(/\+/g, ' '));
}
}
}
return '';
}
function onLoad() {
// Create a custom search control that uses a CSE restricted to
// code.google.com
var customSearchControl = new google.search.CustomSearchControl(
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); // this is the unique search engine ID
var drawOptions = new google.search.DrawOptions();
drawOptions.setAutoComplete(true);
// Draw the control in content div
customSearchControl.draw('results', drawOptions);
customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
// Run a query
customSearchControl.execute(getQuery());
customSearchControl.setLinkTarget(google.search.Search.LINK_TARGET_SELF);
// Maximum length of query is 100 characters
$('.gsc-input').attr('maxlength', '100');
}
google.setOnLoadCallback(onLoad);
</script>
我怎样才能让它停止在 Android 设备上崩溃?让我知道是否需要清除其中任何一个。
【问题讨论】:
-
它是怎么崩溃的?你得到服务器的一些回应了吗?浏览器崩溃?您是否尝试过使用默认页面来代替 /?q=value?
-
实际上,我刚刚发现它为什么会崩溃。它与应用于搜索结果中的文本的自定义 @font-face 字体有关。
标签: jquery android html search