SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can instanceof be used to determine if an object is an HTML element?

    For example:

    Code:
    if (obj instanceof HTMLElement) {...}
    Thanks in advance!

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    You can't use HTMLElement because IE doesn't support it.

    Check for the nodeType property instead. If it's an element then nodeType will equal 1
    If it's not an html element though, nodeType will throw an error so check if nodeType exists first before testing nodeType.

    Code javascript:
    if (obj.nodeType && obj.nodeType === 1) {
        // do element stuff
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    You can't use HTMLElement because IE doesn't support it.

    Check for the nodeType property instead. If it's an element then nodeType will equal 1
    If it's not an html element though, nodeType will throw an error so check if nodeType exists first before testing nodeType.

    Code javascript:
    if (obj.nodeType && obj.nodeType === 1) {
        // do element stuff
    }
    Thanks for the information. I'd like to point out that, although it wouldn't be a factor in my particular application, your solution would return a misleading positive if a user-defined object also implemented nodeType. That said, it's probably the best workaround to be had at present.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    You could always try to use HTMLElement and the instanceof technique, only relying on nodeType if HTMLElement doesn't exist.

    Code javascript:
    function isElement(obj) {
        var isEl = false;
        try {
            isEl = obj instanceof HTMLElement;
    	}
    	catch(err) {
            isEl = obj.nodeType && obj.nodeType === 1;
        }
    	return isEl;
    }

    But then it's up to you as to how many belts and braces you wish to include.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    But then it's up to you as to how many belts and braces you wish to include.
    Now that you mention it, the current project has trended toward more robustness and less bloat than previous efforts. Using the standard where available and a fallback if not seems to fall under robustness, to my present way of thinking.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •