0

I'm adding a search system in JS to my website but it is going wrong. I made an if statement to open another file on my website when the word "music" is the text inside the search bar. I also made another if statement to open youtube.com when youtube is searched. but, when I search youtube, it opens up the file made to open when the search is music. There are no errors. This is the JS code for the if statements and the HTML code for the search bar:

     <script>
            function Searchprocessor() {
if(hfrrfsunehdsurhmurh == "music" || "MUSIC" || "Music" ){
    window.location.replace("search_results/search_music.html");
}
else if(hfrrfsunehdsurhmurh == "Youtube" || "YOUTUBE" || "youtube" || "YouTube"){
    window.location.replace("https://www.youtube.com");
}
            }
        </script>

        <img style="position:absolute; top:0px; right:670px;" src ="images/t.png" height="100">
        <input type="text" id="hfrrfsunehdsurhmurh" style="position:absolute; top:110px; right:650px;" placeholder="Search something">

Please help me get it working.

Chinez
  • 551
  • 2
  • 6
  • 29
Aagney
  • 1
  • 3
  • Simone Rossaini's comment is correct. I'd also like to add that it's best practice to just lowercase your value and compare it once. ex: if(hfrrfsunehdsurhmurh.toLowerCase() === "music") – Ian Dec 30 '20 at 14:16
  • @Ian add your suggest to answer. – Simone Rossaini Dec 30 '20 at 14:20
  • @epascarello you mentioned two links that in my opinion are not suited to the answer to this question. in the first link we speak purely of numbers (index) that do not have much to do with it since it has several identical words. (resolved by Ian's comment). in the second link instead they highlight the comparison between several words that lead to the same result which does not happen here. – Simone Rossaini Dec 30 '20 at 14:28

2 Answers2

0

You always have to repeat the variable you want to compare:

if(hfrrfsunehdsurhmurh === "music" || hfrrfsunehdsurhmurh === "MUSIC" || hfrrfsunehdsurhmurh === "Music" ){
        window.location.replace("search_results/search_music.html");
}else if(hfrrfsunehdsurhmurh === "Youtube" || hfrrfsunehdsurhmurh === "YOUTUBE" || hfrrfsunehdsurhmurh === "youtube" || hfrrfsunehdsurhmurh === "YouTube"){
        window.location.replace("https://www.youtube.com");
}

How is it suggested @Ian it is always better to make it all lowercase / uppercase in order to minimize and speed up the code:

if(hfrrfsunehdsurhmurh.toLowerCase() === "music"){
  window.location.replace("search_results/search_music.html");
}else if(hfrrfsunehdsurhmurh.toLowerCase() === "youtube"){
  window.location.replace("https://www.youtube.com");
}
Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34
  • thanks but i am making a search system so i dont want it to not work if they enter any in uppercase.that is why i did uppercase too – Aagney Dec 30 '20 at 14:41
  • it will work in any case, because it will be transformed later : User write `Music`, system will transform to `music` and compare that `successful` – Simone Rossaini Dec 30 '20 at 14:42
  • i tried this but now when i click the button to search nothing is happening. i used the onclick attribute in the code to make the button with it linking to the function which processes the files.That function was declared before it was used in the onclick. – Aagney Dec 30 '20 at 15:01
0

Another solution apart from @Simone's is to check if an array includes the variable value

if( ["music","MUSIC","Music"].includes(hfrrfsunehdsurhmurh) ){
        window.location.replace("search_results/search_music.html");
}else if( ["Youtube","YOUTUBE","youtube","YouTube"].includes(hfrrfsunehdsurhmurh) ){
        window.location.replace("https://www.youtube.com");
}
HymnZzy
  • 2,669
  • 1
  • 17
  • 27