views:

38

answers:

3

Hi - I'm having what I hope is a simple-to-fix issue.

Basically, I've got one block of javascript containing the function, and then I'm trying to call it from another block of javascript (within a jQuery $(document).ready function). Whilst it works fine on Firefox, I get an 'Object Expected' error in IE7. It's probably something to do with scope, but I'm not sure what to fix. Firebug doesn't seem to give any light on the subject.

So, here's my function:

    <script type="text/javascript">
    //<![CDATA[
    function onsite_validate(){
            $("#tsp_onsite_form").validate({
                errorClass: "form_error",   
                errorElement: "em",
                errorPlacement: function(error, element) {
                    error.prependTo( element.parent("label") );
                },
                highlight: function(element, errorClass) {
                     $(element).addClass(errorClass);
                },
                unhighlight: function(element, errorClass) {
                    $(element).removeClass(errorClass);
                },
                rules: { 
                    fault_found: "required"
                }, 
                messages: {
                    fault_found: "was a fault found?"
                },
                submitHandler: function(form) {
                    $.blockUI();
                    form.submit();

                } //ends submit handler     

            });
        }
//]]>
</script>

and after this, I have the following:

<script type="text/javascript">
//<![CDATA[
$(document).ready(function() {
        onsite_validate();
});
//]]>
</script>

The 'Object Expected' error throws on calling onsite_validate();

I'm sure I'm making a fundamental mistake - just can't seem to spot it!

Many thanks

A: 

I've had this error when trying to load the same javascript file twice (in nested templates). It was difficult to identify because the problem occurred elsewhere in the flow.

What I'm saying is that the error isn't necessarily with that function or even that code block.

Leo
A: 

Sounds like you have a null reference in one of those callback functions.

Just for debugging, you might try checking each of the objects against null and throwing an alert as needed to figure out which object isn't getting set.

For example, check these objects:
element.parent
error
$(element)

JohnFx
+1  A: 

What type of object is error in errorPlacement? Not entirely sure if it's passed as an instance of jQuery, but if not, you might need to work around that.

Edit: just realized you said it works in non-IE. I remember having this error in IE7 only, and having to patch the jQuery source to handle it. What version of jQuery are you using, and are you hosting it yourself or using something like GoogleAPIs? Also, can you provide the exact error (file, line, etc)?

mway