[英]Use javascript and html to submit a search to an external website and open in a new tab
I'm new to the community but I look forward to adding value as I grow in my career as a programmer. 我是该社区的新手,但是我希望随着我成为程序员的职业发展而增值。 I'm a little stuck with something and I'm hoping I can get some help.
我有点受不了了,希望能得到一些帮助。
I'm really close to getting this right but I have one "#" that's throwing me off. 我真的很想解决这个问题,但是我有一个让我失望的“#”。 This is the situation...
就是这种情况
I have a search box on chrome extension pop-up I'm building that is meant to submit its input to the search of another website. 我在建立的chrome扩展弹出窗口上有一个搜索框,用于将其输入提交给其他网站的搜索。 Lets say for example I enter into my searchbox "help" and press enter.
假设例如,我进入搜索框“帮助”,然后按Enter。
The url I go to is this: https://example.com/search/main.action/search/doAdvancedSearch.action?searchQuery.search=help 我转到的网址是: https : //example.com/search/main.action/search/doAdvancedSearch.action?searchQuery.search=help
The url I need to go to is the exact same thing but right after "main.action" I need a "#". 我需要去的URL是完全一样的东西,但是在“ main.action”之后我需要一个“#”。 Like this: https://example.com/search/main.action#/search/doAdvancedSearch.action?searchQuery.search=help
像这样: https : //example.com/search/main.action#/search/doAdvancedSearch.action?searchQuery.search=help
My html code for the form is as follows: 我的表单的html代码如下:
<form target="_blank" action="https://example.com/search/main.action/search/doAdvancedSearch.action" + "?">
<label>Search :
<input type="search" name="searchQuery.search">
</label>
<button type= "submit">Search website</button>
</form>
My script is as follows: 我的脚本如下:
var searchItem = document.getElementById('search').value;
console.log(searchItem);
function website(){
window.open('https://example.com/home/main.action', '_blank');
document.getElementById('q').value = searchItem;
document.getElementById('searchArchived').click();
var results = document.getElementById('search-results').value;
};
document.getElementById('gotowebsite').addEventListener('return', website);
}
I've tried all kinds of things to add the # in the url but I get no luck whatsoever. 我已经尝试过各种方法在网址中添加#,但是我一无所获。 Even putting + "#" after main.action gives me unwanted results.
甚至在main.action之后加上+“#”也会给我带来不必要的结果。 Any suggestions?
有什么建议么? Thanks in advance.
提前致谢。
And yes I've tried using the other suggestions that were given to similar questions. 是的,我已经尝试使用针对类似问题的其他建议。 They didn't address the # problem.
他们没有解决#问题。 :/
:/
This does the job: 这样就可以了:
function redirect() { var searchitem = document.getElementById("search"); window.location.replace("https://example.com/search/main.action#/search/doAdvancedSearch.action?searchQuery.search" + searchitem.value); }
<form action="javascript:redirect()"> <label>Search : <input id="search" type="search"> </label> <button type="submit">Search website</button> </form>
To open the search page in a new tab: 要在新标签页中打开搜索页面,请执行以下操作:
function redirect() { var searchitem = document.getElementById("search"); var url = "https://example.com/search/main.action#/search/doAdvancedSearch.action?searchQuery.search=" + searchitem.value; window.open(url,""); }
<form action="javascript:redirect()"> <label>Search : <input id="search" type="search"> </label> <button type="submit">Search website</button> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.