Free Small / Tiny Javascript Library - updated 8th June 2011
Some people have been grabbing my javascript library from the various sites I have used it on.
Several of these miscreants have then asked me for some explanations of its functioning. {
I am, you will understand joking about 'miscreants' - you are all very welcome to
copy any code of mine whereever you find it]
So I've made some of it available here in case it may be of use or interest for you.
I will just run through a brief synopsis of the library covering the points that have been raised.
Perhaps a few examples of its use, and please feel free to grab either the zipped version or if U prefer, you can copy the text, and paste it into a text file and save it as 'whatever.js'.
If you are a very rich person, you could make a donation and get good karma, or otherwise don't make a donation and still get good karma, But if you are poor please DON'T make a donation (and get good karma).
This is a free server so no php or sql. but if you have a comment, put it in the
yummy page and I will post it here.
Tiny javascript library: Easily editable (by a baby) = Extendable - Support for Flash - Cross Browser support - 3k.
So we made a function to 'get' the object we are interested in and we have returned the object
all ready for chaining.
So we write $(id)
$ = function(a){
O = document.getElementById(a);
...
O.op= function (b) {
if (b){
if (!document.all)
this.style.opacity = b;
else
this.style.filter = 'alpha(opacity=' + b * 100 + ')';
return b;
else {
if (!document.all)
try {
o = this.style.opacity;
} catch(e) {
o = 1
}
else
{
if (!this.currentStyle.filter) return 1;
opac = this.currentStyle.filter.split('=')[1].split(')')[0]/100;
}
if (!o)
o= 1
}
return o;
};
...
return O;
}
Firstly you will notice 'object' is now called 'O' (easier to type)
A plug-in has been added - to get and set opacity. On the getter, the main reason for failure here
is that opacity has not been explicitly set so 'undefined' will typically be returned. In that case it
means its probably 1 (100% for IE) so I’m returning 1.
But very strange things have been know to happen here (beyond the scope of this discourse), hence to try catch clauses.
Anyway, since it's your code, you will know what is most appropriate for your particular
circumstance.
The point is simply to show anyone who is not quite sure, how the plug ins can be
easily added or taken away according to your needs at the time.
So we write $(id).op(.5); set the opacity to 50%.
.
I will put one more in for absolute clarity.
$ = function(a){
O = document.getElementById(a);
...
O.Op= function (b) {
if (b){
if (!document.all)
this.style.opacity = b;
else
this.style.filter = 'alpha(opacity=' + b * 100 + ')';
return b;
else {
if (!document.all)
try {
o = this.style.opacity;
} catch(e) {
o = 1
}
else
{
if (!this.currentStyle.filter) return 1;
opac = this.currentStyle.filter.split('=')[1].split(')')[0]/100;
}
if (!o)
o= 1
}
return o;
};
O.css = function(b,c){
if (!c){
return this.style[b];
} else {
this.style[b] = c;
}
};
...
return O;
}
If you cleaned the code up a bit, you could 'Packer' it. I treat all the sites I'm involved with
as 'works in progress' so I
never do that. Also I'm far tooooo lazy. Also, since the library is so small, server calls
become the main time consuming factor
- I put the site code in the same file as the library for that. You will have your own ideas.
So we write $(id).css("backgroundImage",'url(3_1.jpg)'); Change backgrond image.
Addendum: I have been asked about the O.style[a] in the css plug-in. just to explain then,
It is the same as O.style.whatever except it expects a string as its argument.
Thus if b = 'width', this.style[b] would be like saying this.style.width. OK?