0

I'm receiving an error in firefox error console "Error: submitSearchForm is not defined"

this is my code for that function

EDIT: added full code

   function submitSearchForm(action,iskeyDown) {

                var oneEntered = false;

                    if(iskeyDown == null || iskeyDown == 'undefined'){
                        copyAndValidate("dobFrom", "searchCriteria.dob", "date");
                        copyAndValidate("dobTo", "searchCriteria.dobTo", "date");
                        copyAndValidate("dodFrom", "searchCriteria.dodFrom", "date");
                        copyAndValidate("dodTo", "searchCriteria.dodTo", "date");
                        copyAndValidate("searchCriteria.age", "searchCriteria.age", "integer");
                    }else{
                        copyAndValidate("dobFrom_date", "searchCriteria.dob", "date");
                        copyAndValidate("dobTo_date", "searchCriteria.dobTo", "date");
                        copyAndValidate("dodFrom_date", "searchCriteria.dodFrom", "date");
                        copyAndValidate("dodTo_date", "searchCriteria.dodTo", "date");
                        copyAndValidate("searchCriteria.age", "searchCriteria.age", "integer");
                    }



                    var elements = document.SearchForm.getElementsByTagName("INPUT");
                    for (var i = 0; i < elements.length; i++) {
                        var element = elements[i];
                        if (element != null && element.getAttribute("group") == 'searchCriteria') {
                            if (!isEmpty(element.value)) {
                                oneEntered = true;
                                break;
                            }
                        }
                    }

                if (oneEntered)

                     {

                         if (validate(document.SearchForm)) {

                                    document.SearchForm.action.value = action;
                                      document.SearchForm.submit();
                              }

                } 


                else {
                    alert("<%= bpt.getValue("CCT_ATLEASTONE_MSG") %>");
                }


            }

button

onclick="<%="submitSearchForm('"+SearchForm.ACTION_SEARCH +"');"%>"

just to say again everything works fine in IE so the code is correct

EDIT: VALIDATION.JS validate()

function validate(thisForm) {

   window.event.returnValue = false;
   formToValidate = thisForm;
   var ret = true;
   var validationErrors = new Array();

   // get the validateable items
   // var validateThese = getValidationItems(thisForm.childNodes);
   var validateThese = getValidationItems(thisForm);
   //printValidationArray(validateThese);

   // validate them
   for (var i = 0; i < validateThese.length; i++) {
     var validationItem = validateThese[i];
     var validationError = validateMe(validationItem);
     if (validationError != "") {
       validationErrors[validationErrors.length] = validationError;
     }
   }

   // check for validation errors
   if (validationErrors.length > 0) {
     var errors = "";
     for (var j = 0; j < validationErrors.length; j++) {
         errors += validationErrors[j] + "\n";
     }
     alert("Validation Errors:\n" + errors);
     ret = false;
   } else {
     ret = true;
   }
   return ret;
 }
124697
  • 22,097
  • 68
  • 188
  • 315
  • Is this the full code? Where is window.event used by the code? – David Dec 08 '10 at 16:23
  • i removed the bit about window.event because that wasnt the point of my question. i am asking about submitsearchform. – 124697 Dec 08 '10 at 16:27
  • @user: okay. What piece of JavaScript is causing the _"submitSearchForm is not defined"_ error? Also, why the extra set of braces around your `if (...)` statement? That is syntactically invalid. – Matt Ball Dec 08 '10 at 16:29
  • @matt a button that has onclick=submitSearchForm(...) – 124697 Dec 08 '10 at 16:30
  • @matt no. my bad, i must ave paste those brackets by mistake – 124697 Dec 08 '10 at 16:44
  • Checking out the edit now - but I must say, your comment _"everything works fine in IE so the code is correct"_ is **not true**. IE's JavaScript implementation (called JScript) is not standards-compliant. – Matt Ball Dec 08 '10 at 16:47
  • Your code still looks syntactically invalid. Could you post the actual JS that gets generated, including the `"<%= bpt.getValue("CCT_ATLEASTONE_MSG") %>"` call? If that's part of the JS that the browsers sees, then it definitely will not work, because you're closing that string prematurely, and you should replace one of the quote pairs with single quotes, like this: `alert('<%= bpt.getValue("CCT_ATLEASTONE_MSG") %>');` or `alert("<%= bpt.getValue('CCT_ATLEASTONE_MSG') %>");` – Matt Ball Dec 08 '10 at 16:57
  • @matt even if i remove that alert all together or put a stanrads alert('hello); it doesnt work. edited original post added Validation js – 124697 Dec 08 '10 at 17:18
  • Now that's progress! You're still using `window.event`, which **does not work outside of IE**. You need to **not** do that. See [this question](http://stackoverflow.com/questions/2116177/) (which I linked in my first answer) or [this one](http://stackoverflow.com/questions/3209654/) or [this one](http://stackoverflow.com/questions/3493033/) for alternatives. – Matt Ball Dec 08 '10 at 17:29
  • @user, were you able to solve the problem? – Matt Ball Dec 09 '10 at 23:34

3 Answers3

1

window.event is IE's specific, take a look here for (a bit old) table for different browsers:

Here's the official documentation:

Here's another post for that:

icyrock.com
  • 27,952
  • 4
  • 66
  • 85
  • I dont know why but that error is out of the way now ive added that bit. the problem is...why is it saying the function is undefined if its right there. if that what it means by undefined? that it doesnt exist? – 124697 Dec 08 '10 at 16:25
  • You might have some syntax issues - e.g. if you write: `function abc() {{{{}`, this is going to be a syntax error, regardless of function `abc` "being there". Check you have your `{` and `}` (and other things) matched. For starters, just write: `function submitSearchForm(action,iskeyDown) { alert('hello'); }` – icyrock.com Dec 08 '10 at 16:27
  • 1
    If there is an error in the function definition code, then the function hasn't been defined (because an error occurs when the browser parses it). This is why we're asking for the complete code for the function. – David Dec 08 '10 at 16:30
  • @user521180 Did you try what I suggested in my previous comment? If so, can you revert back with the result of that? I don't see anything obviously wrong with the code you posted, so it might be some other thing, such as: different files being served to different browsers, problem with scope, document compliance, etc. – icyrock.com Dec 08 '10 at 16:58
  • @user521180 - when JS says "function undefined" and you know it does exist, then it means that the function has a syntax error in it (or in the file that contains it) which has prevented JS from interpreting it. This could be something as simple as a reference to an unsupported feature, as per this answer. – Spudley Dec 08 '10 at 17:08
  • @icy checked everything seems to be closed etc – 124697 Dec 08 '10 at 17:15
  • added validation.js to original post – 124697 Dec 08 '10 at 17:19
  • @user521180 Did you change the function to a simple `alert` and confirmed the alert shows as expected when you perform click? Try making a simple button along the lines of `` and clicking it. – icyrock.com Dec 08 '10 at 17:21
1

window.event (also referenced as just event) is not a standard global object in JavaScript. It is an IE-only "feature."

See this question.


Try changing the function declaration to:

function submitSearchForm(action, iskeyDown) {
    // ...
    // { <------------------------------------------------ brace removed
        if (validate(document.SearchForm)) {
            document.SearchForm.action.value = action;
            document.SearchForm.submit();
        }
    // } <-----------------------------------------------  brace removed
}

Braces in JavaScript do not work the same way as in, say, Java. Depending on where they are placed, they mean different things. Example: this question.

I'm guessing the syntax error in the submitSearchForm function declaration is the source of your problem.


I'm still not sure that the code you've posted is actually the code the browser sees, but if so, try this:

function submitSearchForm(action, iskeyDown) {
    var oneEntered = false;

    if (iskeyDown === null || typeof iskeyDown === 'undefined') {
        copyAndValidate("dobFrom", "searchCriteria.dob", "date");
        copyAndValidate("dobTo", "searchCriteria.dobTo", "date");
        copyAndValidate("dodFrom", "searchCriteria.dodFrom", "date");
        copyAndValidate("dodTo", "searchCriteria.dodTo", "date");
        copyAndValidate("searchCriteria.age", "searchCriteria.age", "integer");
    } else {
        copyAndValidate("dobFrom_date", "searchCriteria.dob", "date");
        copyAndValidate("dobTo_date", "searchCriteria.dobTo", "date");
        copyAndValidate("dodFrom_date", "searchCriteria.dodFrom", "date");
        copyAndValidate("dodTo_date", "searchCriteria.dodTo", "date");
        copyAndValidate("searchCriteria.age", "searchCriteria.age", "integer");
    }

    var elements = document.SearchForm.getElementsByTagName("INPUT");
    for (var i = 0; i < elements.length; i++) {
        var element = elements[i];
        if (element !== null && element.getAttribute("group") === 'searchCriteria') {
            if (!isEmpty(element.value)) {
                oneEntered = true;
                break;
            }
        }
    }

    if (oneEntered)
    {
        if (validate(document.SearchForm)) {

            document.SearchForm.action.value = action;
            document.SearchForm.submit();
        }
    }

    else {
        alert('<%= bpt.getValue("CCT_ATLEASTONE_MSG") %>');
    }
}
Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • That didnt work. what did you change other than the alert ' ? – 124697 Dec 08 '10 at 17:13
  • @user: not much. I changed your `== null`'s to `=== null`, and likewise `!= null` to `!== null`. – Matt Ball Dec 08 '10 at 17:17
  • You were right it, it was the window.event declaration in the .js file that was the problem. Even when I set it to false it was still not recognised by firefox and caused all of the problems. Now I completely removed window.event.returnValue = false; and it works. thanks – 124697 Dec 10 '10 at 10:01
0

I had similar error just resolved the same.
The form tag should be under <html><body> tag. e.g. <html><body><form></form></body></html>

If you have just used <form></form> tag and trying to submit then it gives error in older version of mozill while it works in newer version and other browsers.

Krishna
  • 438
  • 5
  • 18