-3

I have a search box on my website. And it was working fine, but suddenly stopped working when i added my websites video page. I keep getting an error saying "Uncaught ReferenceError: search is not defined" and i can't seem to find what the error is

Header.php

<center>
    <input type="text" id="searchbar" onkeydown="search()" style="width:60%;font-size:17px;font-weight:bold; height:40px; padding:0px 10px; margin:3px 0px 0px 0px; border-radius:15px;" placeholder="Search for People, Videos, #hashtags and Blogs..." value="<?php echo $search; ?>"/>
</center>

And my footer page: footer.php

function search(){
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if(keycode == '13'){
        var str = post = document.getElementById("searchbar").value;
        if(str.indexOf("#") == 0){
            var result = str.replace('#', '');
            var result = result.replace(/ /g, '%20');
            window.location.assign("http://www.daparadise.com/testing/daparadise2/search.php?type=hashtag&search=" + result);
        }else if(str.indexOf("people named") == 0){
            var result = str.replace('people named ', '');
            var result = result.replace(/ /g, '%20');
            window.location.assign("http://www.daparadise.com/testing/daparadise2/search.php?type=people&search=" + result);
        }else if(str.indexOf("videos") == 0){
            var result = str.replace('videos ', '');
            var result = result.replace(/ /g, '%20');
            window.location.assign("http://www.daparadise.com/testing/daparadise2/search.php?type=videos&search=" + result);
        }else if(str.indexOf("blogs") == 0){
            var result = str.replace('blogs ', '');
            var result = result.replace(/ /g, '%20');
            window.location.assign("http://www.daparadise.com/testing/daparadise2/search.php?type=blogs&search=" + result);
        }else{
            var result = result.replace(/ /g, '%20');
            window.location.assign("http://www.daparadise.com/testing/daparadise2/search.php?type=web&search=" + result);
        }
    }
}
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
Zacharysr
  • 61
  • 3
  • 11
  • 1
    Just a few thing about your coding style - declare your variables only once, on the top of the function; use === to compare with 0; don't declare variables that you never use (variable post in this case); use regex correctly /\s/ and not / / and finally have a default case if everything fails, as in your case. – hex494D49 Jul 01 '14 at 23:43

2 Answers2

1

It's usually a sign that there is some form of syntax error in your code (in the function or before it) which prevents the Javascript interpreter from reaching the definition of your function. It is then "not defined".

Check your browser logs for Javascript errors, it should point towards the culprit (which is most probably not in the code above).

jcaron
  • 17,302
  • 6
  • 32
  • 46
  • Thank you so much! I looked at the developer area and found i had errors in my javascript BELOW my search function. It works fine now! :) – Zacharysr Jul 01 '14 at 23:29
0

If I copy your code into a Sublime Text document, save it and open the HTML in Chrome…the browser references the line number of the final else clause's declaration and reports:

Uncaught TypeError: Cannot read property 'replace' of undefined

Which corresponds to this assignment:

else {
  var result = result.replace(/ /g, '%20');
  /* etc. */

If all of your if conditions fail, that's the default action…and it can't reference itself in its own initial value definition—it hasn't been defined yet.

Declare var result before entering the sequence of if/else statements, and give it a default value beforehand, too (preferably a String, since you're using String.replace, of course). It can be as simple as:

if(keycode === '13') {
  var result = '';
  var str = /* etc, etc. */
2540625
  • 11,022
  • 8
  • 52
  • 58