Built with 
HomeBrave Tech WorldAbout SiteMarcelo CalbucciMy Videos

Brave Tech World

Entries for July 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);

 




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


July 10, 2006


MON
10
JUL
2006

"Why we fight"

By Marcelo

usa-saddam

    I just finished watching a recent BBC documentary on the Iraq war entitled "Why we fightOpen in a new window" (click to see on Google Video). It is an hour and half of interviews of many people, from generals to pundits, from senators to soldiers.

 

    This is totally worth watching.

 

    I think my perspective has changed since I had a sonOpen in a new window 6 months ago. I feel the world is not moving into a good direction.

 

    This is not an anti-war documentary, but it clearly end up looking like that just because it shows the real side of war. The interesting part is that it removes a lot of the blame for the war on Iraq from Bush. The way it presents this war, Bush is just a puppet, or a consequence, of a policy that started at the end WWII.

 

     I wish we didn't have wars, but, if we are going to have one, it should be for a just cause. A cause that is unquestionably a core value in any society. Spreading democracy is not a just cause. Spreading capitalism is not a just cause.

 

     Here is a hint for the next war: The YanomamiOpen in a new window tribe in Brazil. They are clearly not a democratic or capitalist society for hundreds of years.

 

 

 

 

11:42 AM | Permalink | 2 comments


July 13, 2006


THU
13
JUL
2006

Firefox adds \n to innerHTML!

By Marcelo

    I'm still struggling on trying to find out which browser is best less crappy to develop rich web-based apps.

    Every other day I spend hours trying to figure out a weird behavior that is happening on my system, just to find out that either Firefox or IE has a bug/bad-behavior and now I have to work around it.

    This time is the turn of Firefox. I spent three hours this afternoon trying to find why my TEXTAREAs were returning CR+LF to the server. Then, there is the undocumented WRAP attribute. And I found an extremely good siteOpen in a new window with information about it.

     But after doing every suggestion on this site, or on any other site that I could find out on Google, I figure out that the problem is not with the TEXTAREA, but with the innerHTML attribute.

     TEXTAREA is how I transmit data that is edited in Midas (Firefox's rich HTML editor engine) back to the server, but the way it works is basically like this:

     myTextarea.value = myDiv.innerHTML;

 

     Turn out that Firefox adds some CR+LF to the result of innerHTML, some developer in Firefox probably knows why. But that is super annoying.

     Look at this simple, repro code:

 

  <div id=orig>This is a test to see what firefox will do if you get the innerHTML from a div. Why does it add a Line Feed (\n) to long lines of text?</div>
  <script>
  var di = document.getElementById('orig');
  var str = di.innerHTML;
  alert(str.replace('\n','**N**'));
  </script>

   There is no CR/LF on the DIV, so, you would expect that FF would just display the string as a single line, and no occurrances of **N** on the string, but no, this is the output:

 

This is a test to see what firefox will do if you get the innerHTML**N**from a div. Why does it add a Line Feed (\n) to long lines of text?


Argh!


12:49 PM | Permalink | 1 comment


July 21, 2006


FRI
21
JUL
2006

The best of YouTube (weekly edition)

By Marcelo

 

    I'm a YouTube fanatic. I'll watch hundreds of videos per week (usually just the first 10 seconds, until I release it is not worth my time).

 

    I'm mostly interested in fun small budget (a.k.a. home-produced) videos. But there is also the stuff that you just missed on TV, or you read on MSNBC and want to check YouTube to see if there is a video, etc.

 

   I keep spamming my friends with the links of videos that I found most amusing, usually once a day, but now I decided I'll publish the list of videos that I found cool or interesting to this blog, probably once a week, and probably on Fridays, the official day of "I'm-not-in-a-work-mood-today".

 

   Here is the list for this week:


Nintendo 64:

A very, very excited kid! 
http://youtube.com/watch?v=pFlcqWQVVuUOpen in a new window

 

PS3 vs. XBox 360 vs. Wii:

The battle begins (pretty well produced)
http://www.youtube.com/watch?v=OZJw9sbOTscOpen in a new window

 

John Stewart and John Hodgman (the "I'm a PC" guy) on Net Neutrality

Finally, the make a reference to the Apple commercial.
http://www.youtube.com/watch?v=-YedWtX9tKEOpen in a new window

 

Bush back rub on German Chanceller

I thought wars would start for things like this.
http://youtube.com/watch?v=CE5rx1LTjxIOpen in a new window

 

 

 

8:42 AM | Permalink | 2 comments


Similar Content
Powered by Google