Home > OS >  How to make custom search engine that on enter it goes to google and searches for what the user has
How to make custom search engine that on enter it goes to google and searches for what the user has

Time:01-21

im not very good at html or javascript, but i thought it would be cool to make it so that every time i open a new tab, it is my own searchy thingy or whatever. The problem im having is making it search when I press enter but it never works, I have tried many things but none of them has worked. I honestly have little to no idea to what i'm doing even if I did take a whole class on html in high school. Here is my code.

HTML

    const q = document.getElementById('query');
    const google = 'https://www.google.com/search?q='
    
    
    
    document.addEventListener("keyup", function(event) {
      
        if (event.keyCode === 13) {
         
         const url = google   q.value;
         const win = window.open(url, '_blank'); 
            win.focus();
            alert("test");
                  
        }
    });
body{
background-color: black;
}
    <section>
        <div >
            <input id="query" type="text"  value="" style="background-color:white; color: black; border: none; border-width:0px; text-align:center "/>    

        </div>

CodePudding user response:

Your code will work if you type it like this.

<style>
    .search-box{
        background-color:white;
        color: black; 
        border: none;
        border-width:0px;
        text-align:center;
        border:solid 1px #555555;
    }
</style> 

<input type="text"  onchange = "search(this.value);" placeholder="Type your question?">    
<script>
    if(q==""){
        return false;
    }
    function search(q){
    let Link  = 'https://www.google.com/search?q=' q;
    window.open(Link, '_blank');  
    }
</script>

CodePudding user response:

My bad! The function line should be at the first line. please use the corrected code below:

   <script>
          function search(q){
                 if(q==""){
                        return false;
                 }
        
                 let Link  = 'https://www.google.com/search?q=' q;
                 window.open(Link, '_blank');  
          }
   </script>
  •  Tags:  
  • Related