HTML 和 JavaScript 使搜索输入为 URL 做好准备
HTML and JavaScript make search input ready for URL
我正在制作一个网页,以便我可以从自定义页面搜索谷歌。但是当我单击搜索时,它会在谷歌上搜索我的输入,但是当我输入#和%以及所有这些字符时,URL应该不同。所以我的问题是如何转换这样的东西:
http://www.google.com/#q=Hello World C#
对此:
http://www.google.com/#q=HEllo+world+C%23
使用 JavaScript
编辑:这是我尝试使用的功能
$('button#SubmitSearch').click
(
function()
{
window.location = encodeURIComponent("https://www.google.nl/#q=" + $('input#SearchBar').val());
}
);
和
$('button#SubmitSearch').click
(
function()
{
window.location = "https://www.google.nl/#q=" + $('input#SearchBar').val();
}
);
使用 encodeURI,例如
:console.log(encodeURI('http://www.google.com/#q=Hello World C#'));
> "http://www.google.com/#q=Hello%20World%20C#"
或encodeURIComponent
console.log(encodeURIComponent('Hello World C#'));
> "Hello%20World%20C%23"
从您的 OP :
$('button#SubmitSearch').click(function(){
window.location = "https://www.google.nl/#q=" + encodeURIComponent($('input#SearchBar').val());
});
使用函数encodeURIComponent:
encodeURIComponent('this is a string with special characters like # and ?')
希望对:)有所帮助
试试这个Var search = document.getElementById("searchbox");然后使用 addeventlistener 或 onClick
key=key.replace(//g,"+"); document.location("http://google.com/#q" + search.value);
相关文章:
- 需要URL模板占位符查找和替换功能的输入
- 如何存储&读取输入字段中的任意url
- 替换“;var url”;使用文本框输入
- 在Jquery中将鼠标悬停在图像上后获取输入标记的URL
- 如果输入val=,请转到URL
- 如果未输入URL,则隐藏按钮
- 将输入id传递到jquery ui自动完成中的url
- 验证多个URL输入字段
- 表单输入和选择值将创建一个URL
- I'm试图创建一个html表单,根据字段的输入生成url地址
- 如何从输入到url的路径创建html文件
- Regex来验证texarea输入,该输入必须是用新行分隔的URL
- Chrome webRequest只侦听用户输入的URL
- 如何识别用户在评论框中输入的URL
- 提交的表单重定向到输入URL
- 如何检查“http://”是否包含在 Angular .js 应用程序的 url 输入中
- Javascript或JQuery,用于在URL输入文本框时显示图像
- HTML5 URL输入元素的URL模式或REGEX
- 为从特定URL输入的用户更改页面元素
- 根据URL输入动态更改文本