Dynamic Function Reference in Javascript
January 10, 2007 7:54 AM Subscribe
I'm writing a cross-browser JavaScript library that will allow me to apply events (onmouseover, click, etc) to elements using what look like stylesheets:
How does one reference a function given by a String? Or rather, in
a {mouseover:someFunction;}In writing a parser for these files, I've come upon a stumbling block.
How does one reference a function given by a String? Or rather, in
element.onmouseover = someFunctionhow would one let someFunction be a variable?
eval() maybe ?
Have you looked at the code of the (excellent) competition ?I'm thinking of the Behaviour lib, or LowPro, JQuery, etc
Good luck.
posted by androse at 8:03 AM on January 10, 2007
Have you looked at the code of the (excellent) competition ?I'm thinking of the Behaviour lib, or LowPro, JQuery, etc
Good luck.
posted by androse at 8:03 AM on January 10, 2007
Response by poster: I was hoping not to have to use eval(), simply because of the possibility for misuse.
posted by potch at 8:04 AM on January 10, 2007
posted by potch at 8:04 AM on January 10, 2007
Best answer: Actually, you probably want new Function() instead. Sorry.
posted by grouse at 8:04 AM on January 10, 2007
posted by grouse at 8:04 AM on January 10, 2007
Response by poster: Here's a little more information: The library is written to work hand-in-hand with Prototype.js, and all its buzzworthy-web2.0 goodness.
posted by potch at 8:05 AM on January 10, 2007
posted by potch at 8:05 AM on January 10, 2007
Best answer: I second the recommendation to check out Behaviour. It "uses CSS selectors to apply Javascript behaviours".
posted by purephase at 8:11 AM on January 10, 2007
posted by purephase at 8:11 AM on January 10, 2007
somefunction = function() { alert('it worked') };That's all there is to it.
posted by icebourg at 8:40 AM on January 10, 2007
Best answer: After some digging in the DOM with Firebug, I found that if you have the name of a function in a String, then window[String] returns a reference to the function. Now that it's working, Here's a short little link to the code and examples and stuff. Thanks for the help everyone!
posted by potch at 8:45 AM on January 10, 2007
posted by potch at 8:45 AM on January 10, 2007
Typo?
posted by Deathalicious at 9:19 AM on January 10, 2007
var linkElement = linkTags.item(i); var ajax = new Ajax.Request(linkElement["href"], {onComplete: ActionSheets_Parse}); if (linkElement["rel"] == "JAS") { }Shouldn't it be
var linkElement = linkTags.item(i); if (linkElement["rel"] == "JAS") { var ajax = new Ajax.Request(linkElement["href"], {onComplete: ActionSheets_Parse}); }
posted by Deathalicious at 9:19 AM on January 10, 2007
Response by poster: Deathalicious: yes, it should. Thanks!
posted by potch at 9:21 AM on January 10, 2007
posted by potch at 9:21 AM on January 10, 2007
« Older Travelling to San Francisco for the first time in... | Female leaders of american indian tribes? Newer »
This thread is closed to new comments.
posted by grouse at 8:01 AM on January 10, 2007