使用 javascript 和 html 将搜索提交到外部网站并在新选项卡中打开

Use javascript and html to submit a search to an external website and open in a new tab

我是社区的新手,但我期待在我作为程序员的职业生涯中不断成长。我有点卡住了,希望能得到一些帮助。

我真的很接近正确,但我有一个“#”让我失望。情况是这样的...

我正在构建的 chrome 扩展弹出窗口上有一个搜索框,旨在将其输入提交到另一个网站的搜索中。比方说,我进入搜索框 "help" 并按回车键。

我去的url是这样的: https://example.com/search/main.action/search/doAdvancedSearch.action?searchQuery.search=help

我需要去的 url 是完全相同的,但在 "main.action" 之后我需要一个“#”。像这样: https://example.com/search/main.action#/search/doAdvancedSearch.action?searchQuery.search=help

我的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>

我的脚本如下:

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);


}

我尝试了各种方法来在 url 中添加 #,但我没有任何运气。即使在 main.action 之后放置 + "#" 也会给我带来不想要的结果。有什么建议么?提前致谢。

是的,我已经尝试使用针对类似问题给出的其他建议。他们没有解决 # 问题。 :/

这样就可以了:

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>

要在新选项卡中打开搜索页面:

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>