0

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>
Vivek Jain
  • 2,730
  • 6
  • 12
  • 27
  • Does this answer your question? [Open a URL in a new tab (and not a new window)](https://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-and-not-a-new-window) – Robert Cooper Jul 26 '20 at 06:32

1 Answers1

1

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');
  }
}
Vivek Jain
  • 2,730
  • 6
  • 12
  • 27