Built with 
HomeBrave Tech WorldAbout SiteMarcelo CalbucciMy Videos

Brave Tech World

Week 26
SMTWTFS
1234567

Entries for July 7, 2006


July 7, 2006


FRI
7
JUL
2006

JS Tip #3: Create a Bind/Unbind function

By Marcelo


    This is probably the most useful function on my JavaScript library. Browsers have different behaviors in regards on how to bind, unbind and fire events. So, before you start make sure you create your version (or find an open source library that has one). Here is the very simple version of a bind function:

 

function my_bind(elem, evtname, func)

{

    if(typeof(elem)=="string")

        elem = document.getElementById(elem);

    if(elem.addEventListener)

        elem.addEventListener(evtname, func, false);

    else if(elem.attachEvent)

        elem.attachEvent("on" + evtname, func);

}

 

    Now most people will extend this function to contain some very old syntax to bind events, like:


     elem["on" + evtname] = func;


    This syntax probably dates back to the early IE (3.0?) and Netscape. Nobody uses those browsers anymore. See tip #1 and don't waste your time.

     To unbind, you do the following:


function my_unbind(elem, evtname, func)
{
    if(typeof(elem)=="string")
        elem = document.getElementById(elem);
    if(elem.detachEvent)
        elem.detachEvent("on" + evtname, func);
    else if(el.removeEventListener)
        elem.removeEventListener(evt, f, false);
}

 

 

Memory Leaks

    JavaScript uses a garbage collector to clean up after you leave the page, however, there is a dangerous mix of references between objects in the DOM (like an element) and objects on your JavaScript (like a function) that might cause a cross-reference that the Garbage Collector (GC) doesn’t know how to unwind. The safest way to prevent that from happening is to help the GC by doing some clean up of your own, the most important being to unbind every event that was dynamically bound. My solution is to create an array, and store the information for every bind call, and attach an event onunload of the window to do the clean up.

 

    The code looks like this:


var dynbound = new Array();
function my_bind(elem, evtname, func)
{
   dynbound.push(elem, evtname, func);    
   ...
}

function window_onunload()
{
    for(var i = 0; i < dynbound.length; i+=3)
        my_unbind(dynbound[i], dynbound[i+1], dynbound[i+2]);
    dynbound = null;  
}
my_bind(window, "unload", window_onunload);

 

11:01 AM | Permalink | no comments



FRI
7
JUL
2006

RSS... good; Atom... good; RSS + Atom... bad!

By Marcelo

 

    Here is my problem, why some people care so much about providing an infinite way for people to subscribe to their feed? While just 1 way would be enough.

    Let's start by saying that all Feed reader software can support the latest Atom and RSS technology. Now, you have Flickr, for example that happily provides you with the following formats for you to subscribe:

  • RSS 2.0
  • Atom 1.0
  • RSS 0.91
  • RSS 0.92
  • RSS 1.0

     Really? Couldn't they make a decision? Anyone using RSS 0.91 or 0.92 out there?

 

      Technically, I see no issue with doing that, but it causes a very bad user experience. Take for example this blog that I just subscribe: Matt Cutts: Gadgets, Google, and SEO. Very cool content, but why does Matt provides 3 different feed formats? Couldn't he stick to RSS 2.0 or Atom 1.0? That would be more than enough.

 

     My sites only support RSS 2.0. Never, ever, anyone complained about the lack of Atom.

 

     For Matt, things are a little worse. Look at a screenshot of when I subscribed to his blog on Bloglines -- 13 choices! Not a nice picture for a regular user to choose from ("What if I make a mistake?", "What is the best choice?"):

 

matt

 

 

3:12 PM | Permalink | 1 comment


Similar Content
Powered by Google