spudly.shuoink.com

the best way to predict the future is to implement it

Entries Comments


Category: JavaScript

Pointers / References in JavaScript: a hack

27 December, 2007 (09:49) | JavaScript | 4 comments

I prefer to call them references, but some people call them pointers. Regardless of what you call them, any experienced programmer knows that references sometimes come in handy.
In JavaScript, objects and functions are already passed by reference (as long as you don’t use parenthesis after the function name). Unfortunately, there is no way to force [...]

my version of the popular addEvent()

8 December, 2007 (10:35) | JavaScript | 4 comments

Ok, so here is my version of the popular addEvent() javascript function.
usage:
event_listener.add( object, event_type, function );
Example:
function init() {
alert(”the page is fully loaded”);
}
event_listener.add(window,”onload”, init);
-OR -

event_listener.add(window, “onload”, function init() {
alert(”the page is fully loaded”);
});
Then you can also remove events as well. Remove them in the same manner you created them. Like [...]

more class functions…

6 December, 2007 (19:35) | JavaScript | 6 comments

Update: changed from Object.prototype.hasClass(className) to simply hasClass(element, className) for cross-browser compatibility
Ok… nothing to revolutionary here. This has been done before lots of times. I’ve written two new functions to go along with my hasClass() javascript function.
First is addClass(), which simply adds a class to an object, while preserving the existing classes. Here it is:
function addClass( [...]

Object.hasClass()

1 December, 2007 (21:31) | JavaScript | No comments

Update: For compatibility, Object.hasClass(className) is being changed to simply hasClass(node, className).
Inspired by the dojo toolkit, this function checks to see if a given class is assigned to a node. Simply run it like this:
var my_node = document.getElementsById(”mydiv”);
if( hasClass(my_node, “myclass”) ) {
// …do something
}
Enough talk; here’s the code:
function hasClass( /*HTMLElement:*/node, /*String:*/value ) {
return [...]

the ultimate getElementsBy*: Object.getElementsWhere()

1 December, 2007 (21:14) | JavaScript | 5 comments

Update: for compatibility, Object.getElementsWhere(”condition”) is being changed to getElementsWhere(node, condition) where node is the node to search in.
I’ve looked a lot lately at the getElementsByClass() function and other getElementsBy functions, but they all seemed rather inadequate to me.
I rewrote the function about three times before I figured out what to do. What I really wanted [...]

 Newer entries »