views:

5512

answers:

4

Following is my javascript(mootools) code:

$('orderNowForm').addEvent('submit', function(event){
    event.preventDefault();
    allFilled = false;
    $$(".required").each(function(inp){
        if (inp.getValue() != ''){
            allFilled = true;
        }
    });

    if (!allFilled){
        $$(".errormsg").setStyle('display', '');
        return;
    }
    else{
        $$('.defaultText').each(function(input){
            if (input.getValue() == input.getAttribute('title')){
                input.setAttribute('value', '');
            }
        });
    }

    this.send({
        onSuccess: function(){
                $('page_1_table').setStyle('display', 'none');
                $('page_2_table').setStyle('display', 'none');
                $('page_3_table').setStyle('display', '');
    }
        });
});

In all browsers except IE, this works fine. But in IE, this cause error. I have IE8 so while using its javascript debugger, I found out that the 'event' object does not have a 'preventDefault' method which is causing the error and so the form is getting submitted. The method is supported in case of firefox (which I found out using firebug).

Any Help?

+16  A: 

in IE, you can use

event.returnValue = false;

to achieve the same result.

And in order not to get an error, you can test for the existence of preventDefault:

if(event.preventDefault) event.preventDefault();
Alsciende
The following code worked for me:if (event.preventDefault) { event.preventDefault(); } else { event.returnValue = false; }
Shree
+2  A: 

Check this link

http://mootools.net/docs/core/Native/Event#Event:stop

rahul
+2  A: 

Mootools redefines preventDefault in Event objects. So your code should work fine on every browser. If it doesn't, then there's a problem with ie8 support in mootools.

Did you test your code on ie6 and/or ie7?

The doc says

Every event added with addEvent gets the mootools method automatically, without the need to manually instance it.

but in case it doesn't, you might want to try

new Event(event).preventDefault();
Alsciende
There was some problem when wrapping like this also in IE.Oh my God! Why IE?
Shree
new Event(e).stop(); works in IE6 onwards
Dimitar Christoff
She doesn't want to stop the event, though, just prevent its default action.
Alsciende
A: 

Here's a function I've been testing with jquery 1.3.2 and 09-18-2009's nightly build. Let me know your results with it. Everything executes fine on this end in Safari, FF, Opera on OSX. It is exclusively for fixing a problematic IE8 bug, and may have unintended results:

function ie8SafePreventEvent(e){
    if(e.preventDefault){ e.preventDefault()}
    else{e.stop()};

    e.returnValue = false;
    e.stopPropagation();  
}

Usage:

$('a).click(function(e){
    //Execute code here
    ie8SafePreventEvent(e);      
    return false;

})
Matt