Opera doesn't recognise my event handler bound to a form's "submit" event
Edit: This question is silly, as I had a mistake somewhere else that led to it.
I decided I'd test out my site on Opera today seeing that it works perfectly in Chrome and FF.. So I downloaded the latest Opera for Ubuntu and tried to log into my site. BLEH.
For some reason it is not recognising any functions I bind to the "submit" event of forms, which I need for validation etc. I have the usual addEvent(element, eventtype, callback)
which performs the relevant call to either addEventListener
or attachEvent
.
As I said, this works perfectly in both Chrome and Firefox. I even tried
addEvent(loginForm, 'submit', function(){alert("It works");});
but had no response at all. So it's not even about stopping the default action as I'd first thought, but doesn't actually bind the event at all! With regard to the above example, I can confirm that loginForm
does contain a reference to the actual form element.
Does anybody have any idea what is going on with Opera?
EDIT: Below is my addEvent
function
function addEvent (obj, evt, callback) {
if (evt=="mousewheel")
evt = (/开发者_开发百科Firefox/i.test(navigator.userAgent))? "DOMMouseScroll" : "mousewheel"
if (obj.addEventListener){
obj.addEventListener(evt, callback, false);
} else {
obj.attachEvent("on" + evt, callback);
}
}
Here goes another shameless framework plug...
YAHOO.Util.Event.on( loginForm, 'click', function(e){
alert('It definitely works now!');
} );
Idk if you're keen on adding a bit of YUI/jquery or not :D
I copied your code, see below:
window.onload = function(){
var loginForm = document.getElementById("frm");
function addEvent (obj, evt, callback) {
if (evt=="mousewheel") {
evt = (/Firefox/i.test(navigator.userAgent))? "DOMMouseScroll" : "mousewheel"
}
if (obj.addEventListener){
obj.addEventListener(evt, callback, false);
} else {
obj.attachEvent("on" + evt, callback);
}
}
addEvent(loginForm, 'submit', function(){alert("It works");});
}
And it works. Although I'm currently on a PC in Opera.
Have you tried using Dragonfly to see if there is any errors?
精彩评论