Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Sunday, October 12, 2008

Simple JQuery UID Plugin

UID's can be tricky to make, but here's a simple one for discussion.

I'm posting this to see if anyone can make it a little safer to prevent collisions and/or improve the algorithm. Any thoughts? The prefix allow you to create uids you can select with a prefix on. The random string will give you a fairly large but not unmanageable string. We'll also return the JQuery instance so we can continue our chain.


(function($){
$.fn.uid = function(prefix) {
if (!prefix) {
prefix = "uid";
}
var generate = function() {
var dt = new Date().getMilliseconds();
var num = Math.random();
var rnd = Math.round(num*100000);
return prefix+dt+rnd;
};
return this.each(function() {
this.id = generate();
return $;
});
};
})(jQuery);

Usage:
$().ready(function() {
$('div').uid().css('background-color','blue');
});


Oui? Non?

Thursday, September 11, 2008

How do the Javascript frameworks do it?

Let's take a second and appreciate just how much work must go into building a popular open source JS toolkit.

Even though the domain of the problem is only JS and HTML/CSS the picture isn't as clear as you may think! Besides the technology list you also have to take into account operating system and browsers because, yes, the same browser will behave differently on different operating systems.

In addition to that, we have to also consider doctypes as they change browser rendering, and the toolkit must behave as identical as possible despite different rendering schemes.

So, as an example of the total cost of a single change as of today, September of 2008, it looks like you will have to test (assuming only major browsers and os's):

Windows: IE6, IE7, IE8, FF2, FF3, Chrome 1, Safari 3, Opera 9
MacOSX: FF2, FF3, Opera 9, Safari 3
Linux: FF2, FF3, Opera 9, Konqueror

Sixteen major browser/operating system configurations.

Not done yet:
16 configurations * 6 or so major doctypes (html/xhtml+xhtmldtd with strict/transitional and frameset versions): you end up with

~100 major variations to test after a change.

Yikes you say. Instead of rolling my own components I think I'll just choose a major js toolkit, a widely supported doctype, and learn that!

Smart choice! :)