简体   繁体   中英

how to make an html search bar open a new tab for its results to show

So the the code for an html search bar below loads its results on the same page but how can i make it load its results in a new web tab. Is the any way target="_blank" can work in this scenario?

html


    <div class="box">
      <div class="container-1">
        <input type="search" id="search" placeholder="Search..." />
        <div class="icon" onclick="search(document.getElementById('search').value)"><i class="fa fa-search"></i></div>
      </div>
    </div>

javascript

<script>

    function search(query) {
      if (query.slice(0, 7) == "http://") {
        window.location.href = query
      } else {
        window.location.href = "https://www.google.com/search?q=" + query
      }
    }

</script>

Try this.

function search(query) {
  if (query.slice(0, 7) == "http://") {
    // window.location.href = query
    window.open(query, '_blank');
  } else {
    // window.location.href = "https://www.google.com/search?q=" + query
    debugger;
    window.open("https://www.google.com/search?q=" + query, '_blank');
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM