...

View Full Version : Can one pass arguments with dynamically assigned methods?



]|V|[agnus
08-19-2005, 09:40 PM
I know that assigning functions to events requires you to supply a reference to the function itself, not a call of the function. So is there any way one could pass arguments, or would one have to code a script such that it could perhaps detect differences in its environment instead?

hemebond
08-19-2005, 10:27 PM
An event object is passed by default. You can also access all arguments through the functions 'arguments' property.

]|V|[agnus
08-19-2005, 11:20 PM
Okay, and what does the event object do for me in this circumstance?

Also, that point about the arguments array is great except that it seems to forget the whole premise of my thread, being that one can't pass arguments when assigning functions to events.

So I'm not really sure what you're trying to say.

hemebond
08-20-2005, 12:11 AM
Well, without having any idea what you're trying to achieve, the event object will give you access to the element where you could have certain attributes. Or you could possibly use an anomymous function as the event handler, the contents of which calls another function with arguments.

martin_narg
08-20-2005, 12:19 AM
Example of what hemebond has mentioned:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
function getHref(e) {
var e = e || window.event; // grab event object - try gecko then IE
var el = e.target || e.srcElement; // get element clicked - try gecko then IE
alert(el.getAttribute("href")); // alert the link href
}
</script>
</head>

<body>
<a href="/myPage.html" onclick="getHref(event);return false;">Click me to get the href of this link</a>
</body>
</html>


Hope this helps

m_n



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum