venerdì, aprile 21, 2006

standardizzare setAttribute on IE

setAttribute lavora in maniera strana su IE,
fà quello che deve fare, ovvero cambiare dei valori di alcune proprietà,
ma essendo che vengono modificate le proprietà di un oggetto JScript,
alcune parole come class, for hanno un nome diverso..

dopo aver letto l'articolo su delete.me.uk
ho deciso di utilizzare il metodo ben spiegato per risolvere
i problemi su class,for e style e l'associazione di eventi on...

ho usato un commento condizionale, includendo il codice sotto riportato si ottiene un setAttribute piu' standard.

/*@cc_on
Element = function () {};

Element.prototype.getAttribute = function (attribute) {
if (attribute == "class") attribute = "className";
if (attribute == "for") attribute = "htmlFor";
if (attribute == "style") return this.style.cssText;
else return this[attribute];
}

Element.prototype.setAttribute = function (attribute, value) {
if (attribute == "class") attribute = "className";
if (attribute == "for") attribute = "htmlFor";
if (attribute == "style") this.style.cssText =value;
else
if(value.indexOf("on")!=0)
this[attribute] = function(){eval(value)};
else
this[attribute] = value;
}
var __IEcreateElement = document.createElement;

document.createElement = function (tagName) {

var element = __IEcreateElement(tagName);

element.getAttribute=interface.getAttribute;
element.setAttribute=interface.setAttribute;

return element;
}
var interface = new Element;
onload=function(){
var list=document.all;
var max=list.length;

while(--max)
{
list[max].getAttribute=interface.getAttribute;
list[max].setAttribute=interface.setAttribute;

}
}
@*/
naturalmente per aggiungere un evento nell'onload dovrete poi usare una funzione del genere:

function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof oldonload != 'function') {
window.onload = func;
}
else {
window.onload = function() {
oldonload();
func();
}
}
}

venerdì, aprile 14, 2006

InnerDom

WHY INNERDOM

Some months ago I found that innerHTML is not a standard way to manipulate HTML. Actually it is a proprietary implementation followed by many browser, althought it's very close to a de-facto standard and a lot faster that DOM actions.
So I learnt what could replace innerHTML as the combinations of createElement, insertBefore or the better replaceChild, setAttribute; so I wrote the code this way...

However I felt the need to write HTML directly into a node in a single step.
So I thought and I wrote a function ResToDom that appends an object in eval-notation
down to a node. Recently I felt it wasn't enough.
What I really needed was a function that used that EVAL-Notation object and replace a node (as innerHTML does). I rewrite the whole resToDom into innerDom (i used two different names as the functions are different).

Innerdom takes an object in EVAL notation and tries to convert it into HTML.
this object has 2 special properties:

TAG "the name of the tag to create"
INNER "what tag contains"

If innerDom finds an array it concatenates the nodes into a single node, so if used to write:

document.getElementById("node").innerHTML="aaa <b>some bold text<b/>";

now you can write it as:

innerDom("node",[{TAG:'a',name:'aa'},' ',{TAG:'b',INNER:'some bold text'}]);

Isn't that sw33t?

why EvaL-Notation?

because JSON it nowadays very frequently used joint with AJAx.


Here the Code

lunedì, aprile 03, 2006

Kentaromiura CrazyCorner 0.1 alpha(Gatsu)

I've recentelly find Krazy Corner (www.mswebpeople.com/krazy.html)
and i like some things but there was other things that i don't like,
example if i' ve already a site i don't want to rewrite it all from scratch adding a lot of
extra HTML tag, so i write myself a function that write that code for me ;)

i used span instead of b tag because that seems me better,
the source is HERE :
http://freeforumzone.leonardo.it/viewmessaggi.aspx?f=19716&idd=174

there isn't a live test page avaible yet nor a zip file,sorry but just
put test.html , crazycorner.css and crazycorner.js
in the same directory and enjoy ;)


I don't use innerHTML in this library ,so i should have high compatibility..
I test successful in
Netscape 7.2,
Ie 6.0sp2,
FF1.5, (it should work on 1.0 too because NN have the same gecko)
opera 8.51

..
I'm waiting for a lot of comments!

EDIT:
as Alessandro Fulciniti told me the original idea of Krazy Corners is of Stu Nicholls.(http://www.cssplay.co.uk/boxes/krazy.html)
as soon as possible i release a live-test page