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.
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);
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?"):
I just finished watching a recent BBC documentary on the Iraq war entitled "Why we fight" (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 son 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 Yanomami tribe in Brazil. They are clearly not a democratic or capitalist society for hundreds of years.
I'm still struggling on trying to find out which browser is bestless 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 site 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?
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".