One day, I was trying to detect whether an element is an element that has no close tag.
So I did this:
var tagName = el.tagName.toLowerCase();
if( tagName == ‘img’ || tagName == ‘br’ || tagName == ‘input’ || tagName == ‘embed ‘ ){
// This is just a example, not all single tags are tested. These tags are :
// area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed.
// Do something here.
}
Well, this is not that efficient to write your program.
So I switch to use a string map, which is rather simple and John Resig uses the same trick in his HTML Parser.
var map = makeMap(’area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed’);
if( map[tagName]){
// Do something here.
}
Of course we can use RegExp to test it, just like many others did.
var reg = /^(area|base|basefont|br|col|frame|hr|img|input|isindex|link|meta|param|embed)$/i;
var tagName = ‘BR’;
if( reg.test(tagName)){
// Do something…
}
and maybe you can find more tricks that solve the same problem with low cost, but below is the most interesting one that caught my attention.
if ( tagName != “BUTTON” | “B” | “A” ){
// Do something…
}
Wait, this seems really strange to me and seems not to perform as what I was expecting.
However, since I found this code snippets from MSDN, there must be something magic which makes this working.

Unfortunately, this is simply not working though I almost thought I found a new JS pattern from MSDN and was very excited about it for just about 10 seconds.
It’s fun to learn JS from microsoft.
Vy | 30-Apr-09 at 4:04 pm | Permalink
Doesn’t (oWorkItem.tagName == “BODY”) make the second expression redundant anyway?
And why are they checking for “B” when they’re trying to “make sure menu doesn’t interfere with links and buttons”?
Rob Mueller | 30-Apr-09 at 8:05 pm | Permalink
You’ll have to wait for Perl6 junctions to do that. Bring on the heat death of the universe
http://en.wikipedia.org/wiki/Perl_6#Junctions
Butterfish | 02-May-09 at 2:02 am | Permalink
Thanks for the laugh!
dexbol | 02-May-09 at 2:26 am | Permalink
it’s funny.
WTF!?? what the fuck??