check for HTML* type on object

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

check for HTML* type on object

Post by daedalus__ »

hey i am running to a wall here trying to figure out an objects type. i made a little addChildren function which is great but what if i accidently invoke it on an object that is not an HTMLElement?

oh uhhhh.. this seems kind of silly but heres what i did:

Code: Select all

 
Object.prototype.appendChildren = function () 
    {
        if (arguments.length == 0)
            throw "appendChildren() requires at least one parameter!";
        
        // the magic
        if (!(/HTML/.test(this)))
            throw "appendChildren must be called from an HTML* object.";
        
        for (var i = 0; i < arguments.length; i++)
            this.appendChild(arguments[i]);
    }
 
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: check for HTML* type on object

Post by Darhazer »

typeof ?

You can perform

Code: Select all

typeof this.appendChild != 'undefined'
to check if method exists :-)
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: check for HTML* type on object

Post by daedalus__ »

lol crap. thats a lot better idea :p
Post Reply