AddEventListener Accepting Functions With Arguments?

Jan 29, 2006

I am trying to create a script that will cover cross-browser limitations in adding event listeners to elements. I have found plenty of resources to add a listener but can't find any way of assigning a function containing arguments. Here is what I have so far:

/* Attach events regardless of browser */

function addEvent(obj, evType, fn)
{
if (obj.addEventListener)
{
obj.addEventListener(evType, fn, false);
return true;
}
else if (obj.attachEvent)
{
var r = obj.attachEvent("on"+evType, fn);
return r;
}
else
{
return false;
}}

/* Contains all attachments to be made on page load */

function load()
{
item = document.getElementById('toggleControl');
addEvent(item, 'click', toggle);

item = document.getElementById('alertLink');
addEvent(item, 'click', runAlert);
}

/* Add event to page load and assign load() function */

addEvent(window, 'load', load);

The actual functions for toggle and runAlert are in a seperate .js file, but are standard functions. the problem is that I can't find a way to perform the equivalent of:

item = document.getElementById('toggleControl');
addEvent(item, 'click', toggle('toggleID'));

It simply does not work.

View 10 Replies


ADVERTISEMENT

How To Pass Arguments Independently Or Using AddEventListener

Sep 10, 2002

I use addEventListener and I cannot (or at least don't know how) pass arguments to the function.

Let's say we have a function:
function warning(arg1, arg2) {alert('Argument 1: ' + arg1 + ', Argument2: ' + arg2 + '.');}

It's possible to have:
onclick="warning('my argument 1', 'my argument 1');" as an html attribut.

But I think it's not posible to do it like this (still the same function):
el.addEventListener("click", warning('my argument 1', 'my argument 1'), false);

So how can I pass arguments to the function?

View 1 Replies View Related

Passing Arguments To Callback Function In AddEventListener

Jan 8, 2007

Is there a way to pass arguments to the callback function used inside an addEventListener? I see that I can only list the name of the callback function. For eg, I use this:

var boldLink=document.getElementById('cmtbold');
boldLink.addEventListener("click", rBold, true);

I need the id of boldLink to be accessible from inside the function
rBold()

View 10 Replies View Related

AddEventListener With Member Functions

Sep 20, 2009

I'm having problem setting up event handling using member functions. Consider the following code snippet:

Code:

The problem is that I get a

Code:

View 3 Replies View Related

AddEventListener And Member Functions?

Sep 20, 2009

I'm having problem setting up event handling using member functions. Consider the following code snippet:

function Widget()
{
this.register = function()
{
document.getElementById(this.id).addEventListener('click', this.default_click_handler, false);
}
this.default_click_handler = function(event)

[Code]...

View 2 Replies View Related

Call Two Functions With Arguments Using Onclick?

Oct 20, 2011

need to call two functions say func1(a,b) and func2()i used the following code. But only the first function works, second one not working

Code:
<input type="button" value="Start" onclick="func1('A','B');func2()">

View 2 Replies View Related

JQuery :: Functions With Variable Number Of Arguments?

Jul 26, 2011

So in the Tutorials:How jQuery Works, I've noticed that you can make a call to .get like this : $.get('myhtmlpage.html', myCallBack);

I've also noticed that in the docs jQuery.get() is described as jQuery.get( url, [data,] [success(data, textStatus, jqXHR),] [dataType] ). My question is, in the above example, the callback function is the second argument, whereas in the docs the second argument is data, and the third is the callback function. How does this work? Also, what does it mean when the arguments are laid out in an array-like fashion with commas in the square brackets? i.e. arg1, [arg2,]...

View 4 Replies View Related

Replace Method Takes Functions As Arguments?

Feb 12, 2010

I encountered some code in "Javascript the good part" from Douglas Crockford.There is a way to passing callback function as second argument to string.replace() method.

Code:

var entity = {
quot: '"',
lt: '<',

[code]....

1. How do I know what the number of parameters should be in callback function ( function(a,b) ) ?

2. How do I know what will be passed in as a and b when I am defining the callback function?

View 2 Replies View Related

Count Down Timer Not Accepting Value

May 13, 2011

I'm trying to make a countdown timer, it works fine if I define the value myself from where it has to start. However, when I try to take output from the user it says undefined and 'Nan'.Basically my problem is that if I give c a value like c = 30. it works fine. But when I try to take the input , it gives an undefined error.

View 9 Replies View Related

Js Variable Not Accepting Json String?

Dec 15, 2009

I am slowly working through learning/writing a JS project.here is the core issue.:

var pt =<? echo json_encode($prt);?>
alert(pt) // for diagnostic purposes

json is flattening the PHP array to be used in the script.However, the alert shows me that I only get {[ ,which is just before the first set of double quotes.JS quits at the double quotes, leaving me with an undefined array.How do I fix this?

View 5 Replies View Related

Safari And Chrome Not Accepting GetElementsByTagName?

Aug 11, 2010

I'm using getElementsByTagName() to retrieve some elements and do something with them. It works fine in FF but not in Chrome and Safari.

In Chrome it says: "Uncaught TypeError: Cannot call method 'getElementsByTagName' of null"

Here's the code:

Code:
function popUpSAPWindow(){
// Find all links in the page and put them into an array.
//Below is the line that gives me trouble
var linksInOrderLinesTable =

[Code]....

View 17 Replies View Related

Image Object Src Not Accepting Absolute Paths?

Jul 9, 2010

My page accepts a url with various parameters passed in querystring. One of the parameter is the URL of the Image I wish to display on the page. I save this URL in a variable "imagesource"

To ensure the image is not bigger than the size permitted, I use image object to get the height and width of the image.

Now here's my code:

function foo(){
......
var imagesource = URL of the image;
....
var newImg = new Image();

[Code]....

View 4 Replies View Related

Write A JSP Program Accepting A Single Parameter?

Oct 3, 2009

If the parameter passed is �1�, it will generate an HTML of calculator 1.

If the parameter passed is �2�, it will generate an HTML of calculator 2.

I know how to write a calculator program in HTML format

but I am not sure how to write a jsp and accept a parameter and generate calculator 1/2. code...

View 1 Replies View Related

AddEventListener For IE 5+

Sep 21, 2005

This makes my life a bit easier. After executing this script you should be able to addEventListener on all elements instead of determining if you want to call attachEvent or addEventListener.
Edit: This is the original version. The revised version is below
if (!document.addEventListener && document.attachEvent)
{
Object.prototype.addEventListener = function(eventName, func, capture)
{
if (this.attachEvent)
this.attachEvent('on' + eventName, func);
}

var i, l = document.all.length;

for (i = 0; i < l; i++)
document.all[i].addEventListener = Object.prototype.addEventListener;

window.addEventListener = Object.prototype.addEventListener;
document.addEventListener = Object.prototype.addEventListener;
}

Revised version:

This one is harder to use but it is nicer to the DOM and all newly created objects. The problem with it is that addEventListener will only be available after the page loads.

If you want to use addEventListener from a window.onload script make sure that this code is included in the body, not in the head. document.body.onload is called before window.onload.

Now, only elements that already have attachEvent will get an addEventListener. Elements created with document.createElement will automatically get addEventListener.

function createIEaddEventListeners()
{
if (document.addEventListener || !document.attachEvent)
return;

function ieAddEventListener(eventName, handler, capture)
{
if (this.attachEvent)
this.attachEvent('on' + eventName, handler);
}

function attachToAll()
{
var i, l = document.all.length;

for (i = 0; i < l; i++)
if (document.all[i].attachEvent)
document.all[i].addEventListener = ieAddEventListener;
}

var originalCreateElement = document.createElement;

document.createElement = function(tagName)
{
var element = originalCreateElement(tagName);

if (element.attachEvent)
element.addEventListener = ieAddEventListener;

return element;
}

window.addEventListener = ieAddEventListener;
document.addEventListener = ieAddEventListener;

var body = document.body;

if (body)
{
if (body.onload)
{
var originalBodyOnload = body.onload;

body.onload = function()
{
attachToAll();
originalBodyOnload();
};
}
else
body.onload = attachToAll;
}
else
window.addEventListener('load', attachToAll);
}

createIEaddEventListeners();

View 7 Replies View Related

Any Need For AddEventListener() And AttachEvent() At All?

Nov 16, 2006

Today I have been testing the event models from Netscape 4.8 and IE 4
to the current crop of browsers. I'd like to write a small event
library similar in purpose to the Yahoo! UI event library but with less
features and code. The Yahoo! event library is one of the best
libraries in YUI but it still seems to me to have some confused
code...that or I'm still confused.

The Yahoo! UI library focuses on using addEventListener and
attachEvent. However, due to the click and dblclick bugs in Safari a
long legacy event workaround is included to use a Netscape4-type event
model for Safari. Something like this

var listeners = [function(event){}, function(event){}];
document.getElementById('blue').onmouseover = function(event) {
for (var i=0; i<listeners.length; i++) {
listeners[i](event);
}
};

With this above example, multiple handler functions can be fired for a
single event. I imagine that this is an old trick that has been around
for a long time, yes?

With all the new browsers I tested with this legacy workaround, the
listener handlers can use event.stopPropogation() or
event.cancelBubble=true and they work as desired. The handler functions
can also use event.preventDefault() and event.returnValue=false and
they too work. These seem to work because the event object passed to
the handlers is a modern event object and not one from Netscape4.

My question is, if Safari needs this legacy workaround, and the legacy
workaround seems to work in all the browsers that have addEventListener
or attachEvent, then why bother with the addEventListener and
attachEvent functions at all? Why not just use the legacy way for all
browsers and all type of events.?

View 10 Replies View Related

How To Use AddEventListener Function For IE

Apr 19, 2009

I was using this script to learn how to use event listeners and I need to know how to make it work for IE. I keep finding attachEvent scripts that look like they will work, but I get nothing. I've spent several hours finding script after script that simply don't work. I don't know where to turn next. Any script to attach these events to IE?

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "[URL]">
<html>
<head>
<title>Test</title>
</head>
<body><div>
<script type="text/javascript">

if(!Array.prototype.link)
Array.prototype.link = function(f) { for(var Obect1 = new Array(), i = 0, n = this.length, t = arguments[1]; i < n; i++) Obect1[i] = f.call(t, this[i], i, this);return Obect1;};
Array.prototype.linkMethod = function(m) { var n = this.length, args = this.slice.call(arguments, 1);if(typeof m == "string" && n > 0) m = this[0][m];for(var Obect2 = [], i = 0; i < n; i++) Obect2[i] = m.apply(this[i], args);return Obect2;}; .....

View 2 Replies View Related

AddEventListener In IE Failes

Oct 20, 2006

I'm trying to add a clickevent to an anchor that I created trough DOM.
This his how the code looks:

var oSubLink = document.createElement("A");
oSubLink.appendChild(document.createTextNode("+"));
oCel.appendChild(oSubLink);
oSubLink.addEventListener("click", klapUit(oTabel.id, eigenschappen[2]), false);

It failes at the addEventListener call, saying "No such interface supported" (appears to be one of the two default error messages IE gives when it can't handle your JS :mad: ).

How can I fix this? The solution should work in IE6, FF, Opera, Mozilla and Safari.

View 9 Replies View Related

AddEventListener Parameters

Jul 12, 2007

Is there a way to send parameters to the function being added to an event with addEventListener. I.E. say you have this function

Code:
function someFcn(i){
alert(i);
}
and I add it to an object.

Code:
someElement.addEventListener('focus', someFcn, false);
Is there a way to send a parameter to someFcn.
For Example I have tried this but it failed

Code:
var someString = 'Hello World'
someElement.addEventListener('focus', someFcn(someString), false);

View 3 Replies View Related

AddEventListener In For Loop?

Sep 9, 2011

I have this code:

for(var h:Number=0; h<4; h++){
var Build : Button;
Build = new Button();
Build.height = 20;

[Code]...

View 9 Replies View Related

Using Object Methods In AddEventListener

Jul 20, 2005

When you use addEventListener (or addEvent in IE) to call an object
method, does it call it with the correct this parameter?

The ECMAScript reference has a lot to say about the caller using
Function.prototype.call or Function.prototype.apply and passing the
correct this pointer for the context, but how does addEventListener
determine the correct this pointer. Or does it just punt and pass the
global context, thus making it impossible to refer to the object this
in an object method used as an event listener?

View 6 Replies View Related

AddEventListener For Radio Buttons With Same Id?

Jun 1, 2011

Is it possible to add events using addEventListener to multiple radio buttons with same id ?

<input id="radId" type="radio" value="0" name="radioname">No
<input id="radId" type="radio" value="1" name="radioname">Yes

I tried to attach event using document.getelementByID BUT it always picks the first radio button.

View 3 Replies View Related

AddEventListener() For CtrlEnter Event

Feb 11, 2007

Task: I would like to implement a CtrlEnter event that would work on both IE and FF.

My approach: use addEventListener() and attachEvent() to capture the event then trigger a function to check for CtrlEnter:
if (oTarea.addEventListener) {
oTarea.addEventListener('keyup', function() {checkCtrlEnter(event);}, false);
}
else if (oTarea.attachEvent) {
oTarea.attachEvent('onkeyup', function() {checkCtrlEnter(event);});
}

function checkCtrlEnter(e) {
if (e.ctrlKey && e.keyCode == 13) {
// do something
}
return false;
}

View 5 Replies View Related

Compatibility - AttachEvent Vs AddEventListener Vs Other?

Mar 17, 2003

As I understand it so far:

element.attachEvent()
IE 5+ for PC

element.addEventListener()
Gecko, KHTML

element.event
NS3+, IE4+, dunno about opera or others

Since I'm only concerned with attaching a single event and don't care about bubbling/capturing, am I best off not using the old style of event registering to assure maxium compatibility?

View 2 Replies View Related

Get Function Handle From Name In AddEventListener?

Mar 14, 2010

I have got a following function:

PHP Code:
function addEvent(obj, type, fn) {
if (obj.addEventListener)

[code].....

View 4 Replies View Related

FireFox: AddEventListener Vs. Window.onerror

Nov 30, 2006

I've been using window.onerror to capture and report JavaScript errors
from other users for debugging an application I've written. But I've
run into a strange issue with Firefox and window.onerror.

It seems that any code that executes, having originated from an
"element.addEventListener", causing an error does not activate
"window.onerror". But it does at least show up in Firefox's JavaScript
error console. Internet Explorer doesn't appear to suffer from the
same issue when it uses it's equivalent "element.attachEvent".

Does anyone know why this is and if there is any workarounds or if it's
possibly a bug? Code:

View 2 Replies View Related

JQuery :: AddEventListener Works When Bind Don't?

Oct 22, 2011

i'm loading a simple svg file with the embed html tag:<embed height

='280
' id
='map

[code]....

View 9 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved