473,385 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

i'm having trouble understanding Simon Willison's addLoadEvent()

I thought I understood this article:

http://simon.incutio.com/archive/200...6/addLoadEvent

It seems like a smart, insightful function:
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}

He gives these examples:

addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
addLoadEvent(function() {
/ * more code to run on page load * /
});
Oddly, the first example doesn't work for me at all. I can try

addLoadEvent("checkForm()");

and:

addLoadEvent("checkForm");

but the second form works for me just fine:
addLoadEvent(function() {
//Attaching the onSubmit event to the login form
if (document.getElementById('cmsform')) {
var loginForm = document.getElementById('cmsform');
loginForm.onsubmit = function () {
checkFormForEmptyFields(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(function() {
//Attaching the onSubmit event to the login form
if (document.getElementById('cmsform')) {
var loginForm = document.getElementById('cmsform');
loginForm.onclick = function () {
updateLivePreview(this);
return false;
}
}
});

addLoadEvent(function() {
//Attaching the onSubmit event to the login form
if (document.getElementById('cmsform')) {
var loginForm = document.getElementById('cmsform');
loginForm.onkeydown = function () {
updateLivePreview(this);
return false;
}
}
});

addLoadEvent(function() {
//Attaching the onSubmit event to the login form
if (document.getElementById('cmsform')) {
var loginForm = document.getElementById('cmsform');
loginForm.onchange = function () {
updateLivePreview(this);
return false;
}
}
});

addLoadEvent(function() {
//Attaching the onSubmit event to the login form
if (document.getElementById('cmsform')) {
var loginForm = document.getElementById('cmsform');
loginForm.onblur = function () {
updateLivePreview(this);
return false;
}
}
});
Why is that?

May 18 '06 #1
3 5415
Jake Barnes wrote:
Oddly, the first example doesn't work for me at all. I can try

addLoadEvent("checkForm()");

and:

addLoadEvent("checkForm");


Try: addLoadEvent(checkForm); (which passes the actual function reference,
not a string)
JW
May 18 '06 #2

Janwillem Borleffs wrote:
Jake Barnes wrote:
Oddly, the first example doesn't work for me at all. I can try

addLoadEvent("checkForm()");

and:

addLoadEvent("checkForm");


Try: addLoadEvent(checkForm); (which passes the actual function reference,
not a string)


Thanks much. I guess if I'd been thinking clearly I would have realized
the parameter has to be function, not a string.

Any idea why this code would fail if I try to override two event
handlers on one page element? This works fine:

addLoadEvent(function() {
var refToBody = document.body;
refToBody .onclick= function () {
updateLivePreview(this);
return false;
}
});
but this does not:
addLoadEvent(function() {
var refToBody = document.body;
refToBody .onclick= function () {
updateLivePreview(this);
return false;
}
});

addLoadEvent(function() {
var refToBody = document.body;
refToBody .onblur= function () {
updateLivePreview(this);
return false;
}
});

May 18 '06 #3
Jake Barnes wrote:
Any idea why this code would fail if I try to override two event
handlers on one page element? This works fine:

addLoadEvent(function() {
var refToBody = document.body;
refToBody .onclick= function () {
updateLivePreview(this);
return false;
}
});
but this does not:

addLoadEvent(function() {
var refToBody = document.body;
refToBody .onclick= function () {
updateLivePreview(this);
return false;
}
});

addLoadEvent(function() {
var refToBody = document.body;
refToBody .onblur= function () {
updateLivePreview(this);
return false;
}
});


<URL:http://www.jibbering.com/faq/faq_notes/pots1.html#ps1DontWork>
<URL:http://jibbering.com/faq/#FAQ4_43>

BTW, the `refToBody' variable is redundant.
PointedEars
--
There are two possibilities: Either we are alone in the
universe or we are not. Both are equally terrifying.
-- Arthur C. Clarke
May 24 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Svennglenn | last post by:
I'm working on a program that is supposed to save different information to text files. Because the program is in swedish i have to use unicode text for ÅÄÖ letters. When I run the following...
1
by: ajackson | last post by:
i am having some trouble installing SQl server Reporting Services. well, in order to install the reporting services i have to install Service Pack 3a. so through my installation of package 3a i...
14
by: Joseph | last post by:
I am trying to create a function that allocates memory for the matrix through a function; like the code below. However, this does not seem to work since I believe that the scope of the memory...
7
by: | last post by:
I fail to understand why that the memory allocated in the void create(int **matrix) does not remain. I passed the address of matrix so shouldn't it still have the allocated memory when it returns...
5
by: Krumble Bunk | last post by:
Hello! First things first (but not necessarily in that order), this is a really great group, and has helped me understand more and more C everytime I read the postings, so thanks for a great...
3
by: RSH | last post by:
I am slowly getting the hang of objects and creating my own. I was given some help in assigning an object to a ComboBox collection. I have been looking at it and I get the concept which is very...
18
by: Simon | last post by:
Hi, I understand what one the differences between std::vector, std::deque and std::list is, the std::vector can have data inserted/deleted at the end. The std::deque can have data...
4
by: John Salerno | last post by:
Here's some code from Python in a Nutshell. The comments are lines from a previous example that the calls to super replace in the new example: class A(object): def met(self): print 'A.met' ...
11
by: KraftDiner | last post by:
This is not working the way I think it should.... it would appear that fromfile and getName are calling the baseClass methods which are simple passes.... What have I done wrong? class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.