/    Sign up×
Community /Pin to ProfileBookmark

addEventListener issue

I’m trying to add an event handler for to my body, and for some reason which I can’t figure out, it only works on Google Chrome. I wouldn’t expect it to work in IE, but am wondering why it’s not working in Firefox.

Here’s the relevant parts of the code:

Javascript (in an external file)

[CODE]var body = document.body;
body.addEventListener(“load”, Foo(), false);

function Foo(){
addEventListener(document.getElementsByName(“start”),”click”, alert(“hello”), false);
}[/CODE]

HTML

[CODE]<html>
<head>
<title>BREAKOUT!</title>
<script src=”breakout.js” type=”text/Javascript” >
</script>
<LINK REL=”stylesheet” HREF=”breakout.css” TYPE=”text/css”>
</head>

<body id=”body”>
<!– etc…. –>[/CODE]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Oct 27.2011 — Well, it shouldn't really work at all! Foo() is the return value of the function, in this case nothing. What you actually want is the function, so you just use Foo without the brackets (like #'Foo if you've ever studied Lisp).var body = document.body;
body.addEventListener("load", Foo, false);

function Foo(){
addEventListener(document.getElementsByName("start"),"click", [COLOR="Red"][B]function() {[/B][/COLOR]alert("hello")[COLOR="Red"][B];}[/B][/COLOR], false);
}
The anonymous function is for the same reason, equivalent to lambda in Lisp.

As a matter of interest, John McCarthy who created Lisp died recently, and Lisp and JavaScript share many similarities.
×

Success!

Help @giantpiazza31 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...