我正在使用script page中的javascript搜索引擎,该引擎允许我选择其他提供商/搜索引擎来执行类似google的搜索,但是我尝试在按下发送按钮后在新窗口中打开它,没有成功。
我已经尝试使用:
target="_blank"
<input type="button" value="Send" onClick="startSearch()" target="_blank">
中没有任何反应,或者:<form name="searchForm" target="_blank">
没有!
我也尝试过使用_new而不是_blank,但是也没有用。
最后,我测试了w3schools.com example上的建议,但不幸的是再次失败了。
你们能帮忙吗?谢谢!
最佳答案
快速又肮脏,您可以使用window.open();
方法在新窗口中打开搜索,下面是一些更新的HTML:
<form method="GET" action="#" id="search-form">
<p><label>Search for: <input type="text" id="q" /></label></p>
<p><label>Search from: <select id="engine">
<option selected="selected" value="http://www.google.com/search?q=">Google</option>
<option value="http://www.bing.com/search?q=">Bing</option>
<option value="http://search.yahoo.com/search?p=">Yahoo!</option>
<option value="http://search.aol.com/aol/search?q=">AOL</option>
</select></label></p>
<p><input type="button" value="Send" id="send" /></p>
</form>
和JavaScript:
<script>
var ID = function(str){return(document.getElementById(str));};
ID('send').addEventListener('click', function(){
var search = ID('q').value, select = ID('engine'), engine = select.options[select.selectedIndex].value;
if(!search) {
alert('Please include a search string');
} else {
window.open(engine + search);
}
});
</script>
只要确保在页面末尾调用它,或将其置于
DOMContentLoaded
事件之类的东西中即可。MDN Docs on window.open()