Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
690 views
in Technique[技术] by (71.8m points)

javascript - event not defined in Firefox

I've noticed a couple of other posts on here of a similar nature, but none answer my question, which is why I'm posting this.

I'm trying to make a simple form, whereby each input/textarea element turns red when it is triggered by an onblur effect and the value === "". The following code works fine with Crhome and IE, but in Firefox 'event is not defined' - even though I'm passing through an event.

See below code.

//declare gloabl variables
var srcForm = document.myForm;
var nameField = srcForm.nameTxt;
var emailField = srcForm.emailTxt;
var commentsField = srcForm.comments;

//function for field onfocus events
function focusClear (event) {
var eSrc = nuahs.eventUtility.eventSource(event);
if(eSrc.className === "invalid") {
    eSrc.className = "";
    eSrc.value = "";    
}
}

//function for field onblur events
function blurCheck (event) {
var eSrc = nuahs.eventUtility.eventSource(event);
if(eSrc.value === "") {
    eSrc.className = "invalid";
    eSrc.value = "This field is required";  
}
}

//set up event handlers for focus events on each field
nuahs.eventUtility.addEvent(nameField, "focus", function() { focusClear(event)});
nuahs.eventUtility.addEvent(emailField, "focus", function() { focusClear(event)});
nuahs.eventUtility.addEvent(commentsField, "focus", function() { focusClear(event)});

//set up event handlers for focus events on each field
nuahs.eventUtility.addEvent(nameField, "blur", function() { blurCheck(event)});
nuahs.eventUtility.addEvent(emailField, "blur", function() { blurCheck(event)});
nuahs.eventUtility.addEvent(commentsField, "blur", function() { blurCheck(event)});

So, you can see - at the bottom - I'm handling my events with the blurCheck and focusCheck functions, and passing in the event. Therefore I have not a clue why FF does not recognize it!!

The nuahs object is just a simple event utility in another script, but for completeness you can see it below:

var nuahs = new Object();

nuahs.eventUtility = {
addEvent: function() {
    if(typeof addEventListener !== "undefined") {
        return function(obj, evt, fn) {
            obj.addEventListener(evt, fn, false);   
        };
    } else {
        return function(obj, evt, fn) {
            obj.attachEvent("on" + evt, fn);    
        };
    }
}(),
removeEvent: function() {
    if(typeof addEventListener !== "undefined") {
        return function(obj, evt, fn) {
            obj.removeEventListener(evt, fn, false);    
        };
    } else {
        return function(obj, evt, fn) {
            obj.detachEvent("on" + evt, fn);    
        };
    }
}(),
eventSource: function(event) {
    if (typeof addEventListener !== "undefined") {
        return event.target;    

    } else {
        return event.srcElement;    
    }
},
preventDefault: function() {
    if (typeof addEventListener !== "undefined") {
        return function(event) {
            event.preventDefault(); 
        }
    } else {
        return function(event) {
            event.returnValue = false;  
        }
    }
}(),
stopEvent: function() { //to stop event capture and bubbling
    if (typeof addEventListener !== "undefined") {
        return function(event) {
            event.stopPropagation();    
        }
    } else {
        return function(event) {
            event.cancelBubble = true;
        }
    }
}()
} 

Any help VERY much appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You forgot to pass around the event object. It's only a global in old IE versions.

nuahs.eventUtility.addEvent(nameField, "focus", function(event) { focusClear(event)});
nuahs.eventUtility.addEvent(emailField, "focus", function(event) { focusClear(event)});
nuahs.eventUtility.addEvent(commentsField, "focus", function(event) { focusClear(event)});

Or even better, to preserve this in case your utility library sets it to a proper value:

nuahs.eventUtility.addEvent(nameField, "focus", focusClear);
nuahs.eventUtility.addEvent(emailField, "focus", focusClear);
nuahs.eventUtility.addEvent(commentsField, "focus", focusClear);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...