Posts

Showing posts from December, 2008

External selectors as engines and how to create your own library

With " Sizzle event", the challenge about libraries will move from the coolest/fastest selector engine into the coolest/fastest way to use it for library purpose. Since Sizzle will be just one of them, thanks to our natural behavior ( read: re-create the wheel ) it is possible that there will be more selector engines around the net. So, why we could not create our own library that does just what we need and nothing else, maintaining a decent size and performing like a jaguar :D ??? Here a truly basic example about how to create your own library, basing it over a generic selector engine or generic library (or just its engine, if you prefer one) // our wonderful library constructor function myQuery(){ // just in case we prefer another name for the constructor // this code is "name proof" var callee = arguments.callee; return this instanceof callee ? this : callee.prototype.init.apply(new callee, arguments) ; }; // add some...

Visual Studio 2008 - The good part

... whatever I said in the precedent post, at least VS9 let us extend it and create macros as well. Unfortunately, I could not create a macro in C# so I had to use a language that I do not like at all, Visual Basic, and the result is an alpha macro that allows us to create "more clever" regions and outline for brackets as well. More clever regions There is another macro that goes around since Visual Studio 2005 but it has some bug and/or problem with nested regions plus it does not allow us to write directly in the region what the region is about //#region whatever ... //#endregion --- to obtain //#region whatever[...] --- instead of // another "boring" comment over the region [...] At the same time that macro put every region, nested or not, at the beginning of the line. It is not such a big problem, but I prefer this version. Brackets outline This is absolutely experimental and there are hundreds of problems, but for a couple of files it is doing its job. What...

Visual Studio 2008 and JavaScript: the worst couple ever

We are using Visual Studio 2008 Team Edition at work and it is great for our C# plus SQLServer application, but I am working entirely with JavaScript and every time my files grow up " too much " I start having headache scrolling up and down thousands of line of code. A basic function that even a small editor like Notepad++ has, the plus/minus sign to close functions closures, does not exists. You should try to create your own macro, in VB "of course", and call it every time. As alternative, I added the file extension js in the option with HTML Editor as default editor to let me do something like this //#region MyConstructor <script> ... my code //#endregion </stript> Above start and end comment allows me to close code blocks and everything inside will be highlighted because the editor consider it as an HTML JavaScript tag: it's true, HTML has open/close blocks while JavaScript does not. The " fantastic " debugger is nothing that better tha...

outerHTML for almost every browser ... if you need it ...

We all have to deal with memory leaks problem and apparently a good way to be sure that an HTMLElement has been removed from its "flow" is the outerHTML assignemnt (thanks Ariel for the suggestion) element.outerHTML = ""; element = null; If the element is not a document.body or another body parent node, Internet Explorer will "extract" that element from its context, if any, and if there are no other references for that element the null assignment will, theoretically, complete the opera, hopefully solving memory leaks problem for that node as well ... To remove ... but to replace too ... Every library has a " swap " or replace method to quickly change an element, but even if we all know that innerHTML is the fastest way to insert content, few libraries use outerHTML to replace nodes , that as far as I know, should be " that fast " in IE as innerHTML is: // traditional swap, the DOM way function swap(oldNode, newNode){ var parentNo...

A fast and crossbrowser function to make an Array

An absolutely common task present in almost every library, is to transform a generic collection/list of objects or DOM Elements into a friendly Array. Cases Scenario It is possible to apply directly an Array.prototype.slice call to quickly return an Array It is not possible at all apply every kind of Array prototype to the list Cases scenario detection When we execute a piece of JavaScript in a web page, we can assume that we have at least one DOM element in that page, as the script itself for example, so it is always possible to obtain a collection of elements calling document.getElementsByTagName("script") or a generic ("*") in this case probably superflous considering the latter assumption. Cross browser and fast makeArray proposal var makeArray = function(push, slice){ try{ // Andrea Giammarchi proposal slice.call(document.getElementsByTagName("script"), 0); return function(array, results){ array = array instan...

Stressfull procedure? Distribute your task!

Have you never been in troubles with frozen GIFs or unresponsive HTML? Sometimes JavaScript could be used to perform really stressful task and a loop, a for in, or an each, could not be fast enough to make your DOM responsive. What we need in this case is simply a closure to make sure references are consistent and our job will end up in the correct order. This is a probably silly but I hope interesting function to make the DOM and generally the page more responsive: Time = { setTimeout:function(Stack, delay){ var self = this; if(!delay) delay = 1; if(!(Stack instanceof Array)) Stack = [Stack]; setTimeout(function(){ Stack.shift().call(self); if(0 setTimeout(arguments.callee, delay); }, delay); } }; We can call this functon in different ways, stating from the demo: Time.setTimeout([ function(){ alert("Hello"); }, function(){ alert("Di...