Monday, August 23, 2010

HTML Comments as strings

Reading HTML comments from a DOM tree with JavaScript is easy.

var nodes = document.body.childNodes,
comments = [];

for (var i = 0; i < nodes.length; i++) {

// if the nodeType is 8
if (nodes[i].nodeType == 8) {

// this is a comment
comments.push(nodes[i]);
}
}

Let's say that the comments array is not empty, so the type of comments[0] is a Comment. That's great but how can you read its content?

This is what the W3C DOM-Level-1 spec says. It implements the Comment interface.

interface Comment : CharacterData {
};

This does not really help us but don't despair. It also implements the Node interface.

...
readonly attribute DOMString nodeName;
attribute DOMString nodeValue;
...

Yes, it's read-only but you can get the comment as a string by using its nodeValue.

comments[0].nodeValue


W3C DOM-Level 3 specifies something called textContent. This can also do the job, you can read about them here, textContent vs. innerText

Thursday, August 5, 2010

Blame does not fix bugs

It's always seems hard to deal with people. Here are few thoughts that I'd like to share.

Blame does not fix bugs, the outcome is important, not the credit, the blame, or the ongoing intellectual superiority contest.
You want to focus on fixing the problem, instead of blaming others.

Criticize ideas, not people, keep it professional, not personal.
Ask questions that allows someone to figure out the problem for themselves.

and there is one more thing, negativity kills innovation so be constructive.