Stopping a form submit event in Safari

Joined: 11/28/2008

I've been banging my head on the keyboard and I'm still not any closer to figuring this out. I've got this:

CODE
Event.observe('emailmeform', 'submit', sendemail, false); // event listener
var foo = $('emailmeform'); // basically document.getElementById
foo.onsubmit = function() {return false;}; // returns false in everything but Safari

Yet Safari won't stop the event. So I threw an alert() into there:

CODE
Event.observe('emailmeform', 'submit', sendemail, false);
var foo = $('emailmeform');
foo.onsubmit = function() {alert('bla'); return false;};

And the alert isn't executing either, so I can't figure out why Safari won't execute the line?

Joined: 11/28/2008
Sorry Elliot, but by now you

Sorry Elliot, but by now you have to expect this kind of answer from me /wink.gif" style="vertical-align:middle" emoid=";)" border="0" alt="wink.gif" />
Try this:

CODE
/*
  * Dependencies
  * yahoo.js
  * event.js
*/
YAHOO.util.Event.on('emailmeform','submit',function(e) {
    YAHOO.util.Event.stopEvent(e);
  }
);

Works in Safari like a charm /biggrin.gif" style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" />

Joined: 11/28/2008
Haha...I thought that's the

Haha...I thought that's the kind of response I'd get.

Though, I wrote the rest of the script over Prototype and I really don't want to throw another one in (especially as once I get this bug fixed I thought I might just upload it and distribute it as a package). It's actually my contact form, you can see it in action over there.

Prototype does have an Event.stop(), but it doesn't work in Safari either. What I just posted up top is what Particletree recommended, and strangely enough it does seem to work with the click event, but not a submit.

Joined: 11/28/2008
Oh Dustin, I went through the

Oh Dustin, I went through the YUI Event Utility and grabbed it's stop event function, threw it in my script...and it worked in all browsers except for Safari. You can see it for yourself on the page I linked to in the post above.

Is there some weird 'submit' event bug in Safari that everybody knows about but me?

Joined: 11/28/2008
seems to work just fine for
Joined: 11/28/2008
Safari doesn't drop the

Safari doesn't drop the default event handler for onclick unless you explicitly set obj.onclick = something/null. It's a bug in the browser, quite well known and has been posted about here before. I'm pretty sure that's what the Yahoo! code does with Safari (and Konqueror too).

Paul Davey
Whitford Church
"Everyone who calls on the name of the Lord will be saved." Romans 10:13
"For all have sinned and fall short of the glory of God, and are justified

Joined: 11/28/2008
I'm pretty sure that's
QUOTE
I'm pretty sure that's what the Yahoo! code does with Safari (and Konqueror too)

Yes. That is what they did.

For the event, it was basically onEvent = false; So in this case, it is onsubmit = false;

Joined: 11/28/2008
Then why didn't this

Then why didn't this work?

CODE
var emailformClick = $('emailmeform');
emailformClick.onsubmit = function() {return false;};

I also tried this:

CODE
    // the following preventDefault function is from the YUI Event Utility and is Copyright (c) 2006, Yahoo! Inc. All rights reserved.
    stop        :    function stop(ev)    {
                    if (ev.preventDefault) {
                    ev.preventDefault();
                    } else {
                        ev.returnValue = false;
                    }
                    },

Then I called this.stop(event) from another method. Both of these worked in FF, Opera, and IE...but not Safari.

Joined: 11/28/2008
this really is the most

this really is the most trivial of all things. Try this:

HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns='http://www.w3.org/1999/xhtml' lang='en' xml:lang='en:us'>
<head>
<title>stopping an event in safari</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
<!--
window.onload = function() {
test.onsubmit = function() {
return false;
};
};
//-->
</script>
</head>
<body>
<form id='test' name='test' method='get' action='stop-event-safari.html'>
<input type='text' value='test' />
<input type='submit' value='Submit' />
</form>
</body>
</html>

Turns out you don't even need an HTMLElement Reference. I just straight up put test.onsubmit (assuming it's getting 'test' from the name