My JavaScript book is out! Don't miss the opportunity to upgrade your beginner or average dev skills.
Showing posts with label cross-browser. Show all posts
Showing posts with label cross-browser. Show all posts

Friday, October 23, 2009

Node sourceIndex for every browser

I was doing a couple of tests in my bloody Android and I don't even know how I ended up with such snippet ... anyway, accordingly with @ppk table sourceIndex is a node property compatible with every version of Internet Explorer and latest Opera browser but not with Chrome, Firefox, or Safari (WebKit Android and iPhone as well).

Being these unsupported browsers compatible with both native Array.prototype.indexOf and __defineGetter__ and being 5 to 100 times faster than IE, I thought: why don't put this as well into vice-versa?

/** sourceIndex by vice-versa - Mit Style License
* @target FireFox, Safari, Chrome
*/
if(typeof document.documentElement.sourceIndex == "undefined")
HTMLElement.prototype.__defineGetter__("sourceIndex", (function(indexOf){
return function sourceIndex(){
return indexOf.call(this.ownerDocument.getElementsByTagName("*"), this);
};
})(Array.prototype.indexOf));
;

I so much would have cached the live getElementsByTagName("*") object then I thought there must be a reason if different libraries pass via node.ownerDocument rather than direct document ... isn't it? Well, if not I'll change soon but right now I am pretty much happy with performances :)

Why sourceIndex


If we would like to know, as example, if a node is before or after another one (considering that nested nodes will be considered always after their container) I bet there's nothing simpler than:

if(a.sourceIndex < b.sourceIndex){
// move up b, move down a
} else {
// move up a, move down b
}

... or something similar, enjoy

Update
Apparently in jQuery dev list somebody already talked about analogue snippets (thanks @diegoperini) but it's not my idea to normalize anything, respecting what sourceIndex should do: compare index in the full HTMLCollection and nothing else, the rest could be easily implemented over this snippet :)

Thursday, September 10, 2009

Formaldehyde JS - The Circle Is Close

As announced in Ajaxian, I have created Formaldehyde JS with exactly the same Zero Config logic.

We put this file before everything else and that's it, Formaldehyde will automatically decide how to show errors in a wide range of browsers:
  • Chrome
  • Firefox
  • Internet Explorer 5, 5.5, 6, 7, 8 with or whout console
  • Opera
  • Safari

Logs will naturally degrade until the most primitive alert but hey, Formaldehyde is for debug and development environments, not for production.

Enjoy ;)

Wednesday, July 15, 2009

The Fastest Date toJSON and fromJSON?

I know yesterday post supposed to be the last 'till the the end of the month but I could not resits. Dustin Diaz twitted

anyone trying to parse our wonky rails dates in JS. do this: var date = Date.parse(str.replace(/( \+)/, ' UTC$1')); seems to fix it.

Again, I could not resist to create a toUTCString based function to create JSON strings and to parse them back. This is the result and apparently is both the fastest implementation I know and cross browser (Chrome, Firefox, Inernet Explorer, Safari).
This is a quick post so if you find some problem please let me know, thanks.

(function(){
// WebReflection Fast Date.prototype.toJSON and Date.fromJSON Suggestion
var rd = /^.{5}(\d{1,2}).(.{3}).(.{4}).(.{2}).(.{2}).(.{2}).+$/,
rs = /^(.{4}).(.{2}).(.{2}).(.{2}).(.{2}).(.{2}).+$/,
d = new Date,
f = /(GMT|UTC)$/.exec(d.toUTCString())[1], // cheers gazheyes
m = {},
i = 0,
s = ""
;
d.setUTCDate(1);
while(i < 12){
d.setUTCMonth(i);
m[s = /^.{5}(\d{1,2}).(.{3})/.exec(d.toUTCString())[2]] = ++i < 10 ? "0" + i : i;
m[m[s]] = s;
};
Date.prototype.toJSON = function(){
var e = rd.exec(this.toUTCString());
return e[3].concat("-", m[e[2]], "-", e[1], "T", e[4], ":", e[5], ":", e[6], "Z");
};
try{Date.fromJSON(d.toJSON());
Date.fromJSON = function(s){
var e = rs.exec(s);
return new Date(Date.parse(e[2].concat(" ", e[3], " ", e[1], " ", e[4], ":", e[5], ":", e[6], " ", f)));
};
}catch(e){
Date.fromJSON = function(s){
var e = rs.exec(s);
return new Date(Date.parse(e[3].concat(" ", m[e[2]], " ", e[1], " ", e[4], ":", e[5], ":", e[6], " ", f)));
};
}
})();

Basically everything is a shortcut and I am using only native methods ... is there anything faster except probably a substr based one?

Friday, March 20, 2009

noSWFUpload - Zero Plug-In Multiple Upload

As announced a couple of days ago, I am finally proud to introduce my last library free Multiple Upload Component, now compatible with every browser and always without usage of 3rd parts plug-ins.


This little project has been hosted in Google Code and I promise I'll write there a complete documentation asap. Righ now, you can read the project source code via zip to understand what he little API is doing.

Update I added a couple of sections in the Wiki page ;)

Compatibility?
  • Internet Explorer 5.5 (probably 5 too), 6, 7, and 8
  • FireFox? 3 or greater
  • Google Chrome 1.0
  • Opera 8, 9, and 10
  • Safari 4 (current beta)
  • other browsers via unobtrusive iframe


Try the demo if you do not believe it :P