Forum Moderators: open

Message Too Old, No Replies

Stop addEventListener from executing onload when function w/parameters

Adding parameters to function being called incorrectly executes onload!

         

JAB Creations

12:04 pm on Dec 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've setup an addEventListener in an unobtrusive manner.

The issue is whenever I add any parameters to the function I want to execute on the event all browsers (IE, Gecko, Opera, Webkit) then decide to execute it during the onload event! The addEventListener events must be called during onload correct? So why the heck does the act of adding parameters automatically make the event listeners trigger during the onload event? This completely negates the point of having the event listeners! :(

The following script is loaded via the onload event...

if (!document.all) {document.getElementById('menua5k').addEventListener('click', test_menus, false);}
else {document.getElementById('menua5k').attachEvent('onclick', test_menus);}

Here is the exact same code (each) with a simple parameter...

if (!document.all) {document.getElementById('menua5k').addEventListener('click', test_menus('hi there!'), false);}
else {document.getElementById('menua5k').attachEvent('onclick', test_menus('hi there!'));}

- John

JAB Creations

12:53 am on Dec 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I found another way to accomplish this.

- John

function test_menus(example)
{
alert(example);
}

function start_onload
{
if (window.addEventListener)
{
var object = document.getElementById('menua5k');
object.addEventListener('click',function() {test_menus('hello');},false);
object.myflag = 'test success!';
}
else
{
var object = document.getElementById('menua5k');
object.attachEvent('onclick',function() {test_menus('hello');});
object.myflag = 'test success!';
}
}