473,241 Members | 1,405 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,241 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 5410
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.