I thought I understood this article:
It seems like a smart, insightful function:
function addLoadEvent(fu nc) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}
He gives these examples:
addLoadEvent(na meOfSomeFunctio nToRunOnPageLoa d);
addLoadEvent(fu nction() {
/ * more code to run on page load * /
});
Oddly, the first example doesn't work for me at all. I can try
addLoadEvent("c heckForm()");
and:
addLoadEvent("c heckForm");
but the second form works for me just fine:
addLoadEvent(fu nction() {
//Attaching the onSubmit event to the login form
if (document.getEl ementById('cmsf orm')) {
var loginForm = document.getEle mentById('cmsfo rm');
loginForm.onsub mit = function () {
checkFormForEmp tyFields(this);
return false;
}
}
});
However, I want to add an event to several of the event handlers of
this form. Yet when I try, the code stops working, and I get the
amazing error message that loginForm has no properties.
This doesn't work, but I can't imagine why:
addLoadEvent(fu nction() {
//Attaching the onSubmit event to the login form
if (document.getEl ementById('cmsf orm')) {
var loginForm = document.getEle mentById('cmsfo rm');
loginForm.oncli ck = function () {
updateLivePrevi ew(this);
return false;
}
}
});
addLoadEvent(fu nction() {
//Attaching the onSubmit event to the login form
if (document.getEl ementById('cmsf orm')) {
var loginForm = document.getEle mentById('cmsfo rm');
loginForm.onkey down = function () {
updateLivePrevi ew(this);
return false;
}
}
});
addLoadEvent(fu nction() {
//Attaching the onSubmit event to the login form
if (document.getEl ementById('cmsf orm')) {
var loginForm = document.getEle mentById('cmsfo rm');
loginForm.oncha nge = function () {
updateLivePrevi ew(this);
return false;
}
}
});
addLoadEvent(fu nction() {
//Attaching the onSubmit event to the login form
if (document.getEl ementById('cmsf orm')) {
var loginForm = document.getEle mentById('cmsfo rm');
loginForm.onblu r = function () {
updateLivePrevi ew(this);
return false;
}
}
});
Why is that?
It seems like a smart, insightful function:
function addLoadEvent(fu nc) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}
He gives these examples:
addLoadEvent(na meOfSomeFunctio nToRunOnPageLoa d);
addLoadEvent(fu nction() {
/ * more code to run on page load * /
});
Oddly, the first example doesn't work for me at all. I can try
addLoadEvent("c heckForm()");
and:
addLoadEvent("c heckForm");
but the second form works for me just fine:
addLoadEvent(fu nction() {
//Attaching the onSubmit event to the login form
if (document.getEl ementById('cmsf orm')) {
var loginForm = document.getEle mentById('cmsfo rm');
loginForm.onsub mit = function () {
checkFormForEmp tyFields(this);
return false;
}
}
});
However, I want to add an event to several of the event handlers of
this form. Yet when I try, the code stops working, and I get the
amazing error message that loginForm has no properties.
This doesn't work, but I can't imagine why:
addLoadEvent(fu nction() {
//Attaching the onSubmit event to the login form
if (document.getEl ementById('cmsf orm')) {
var loginForm = document.getEle mentById('cmsfo rm');
loginForm.oncli ck = function () {
updateLivePrevi ew(this);
return false;
}
}
});
addLoadEvent(fu nction() {
//Attaching the onSubmit event to the login form
if (document.getEl ementById('cmsf orm')) {
var loginForm = document.getEle mentById('cmsfo rm');
loginForm.onkey down = function () {
updateLivePrevi ew(this);
return false;
}
}
});
addLoadEvent(fu nction() {
//Attaching the onSubmit event to the login form
if (document.getEl ementById('cmsf orm')) {
var loginForm = document.getEle mentById('cmsfo rm');
loginForm.oncha nge = function () {
updateLivePrevi ew(this);
return false;
}
}
});
addLoadEvent(fu nction() {
//Attaching the onSubmit event to the login form
if (document.getEl ementById('cmsf orm')) {
var loginForm = document.getEle mentById('cmsfo rm');
loginForm.onblu r = function () {
updateLivePrevi ew(this);
return false;
}
}
});
Why is that?
Comment