Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to know if an object is an Html document using javascript

I'm passing an object say, obj, to a function. obj could possibly of any type - (TemplatedHelper, AlertMessage, PartialViews, HTMLDocument, etc.) I want to know if obj is an HTML Document. What are the possible ways to achieve it?

I have tried using

    var containerCount = $(obj).length;
      for (var ctr = 0; ctr < containerCount; ctr++) {
        var containerTagName = $(obj)[ctr].tagName;
        alert(containerTagName); // to know all detected tagNames
                                 // this returns LINK, SCRIPT, DIV, INPUT, etc..                   
        if ((containerTagName == "TITLE") || (containerTagName == "HTML")) {
          var isHTML = true;
          break;
        }
      }

with the preceding code, Chrome only detects the title tag, but IE8 doesn't detect html, head, and title tags. While these fragment codes don't work in IE8 too:

    alert($(obj).has('title')); // or 'html' as element parameter, returns [object Object]
    alert($(obj).find('title')); // or 'html' as element parameter, returns [object Object]
    if ($(obj)[ctr].parent())  
      alert($(obj)[ctr].parent().get(0).tagName); // returns undefined

Please share me your thoughts about it. Thanks in advance!

like image 787
ideAvi Avatar asked Jan 18 '26 21:01

ideAvi


1 Answers

Try this:

if (obj instanceof HTMLDocument)
{
    // obj is a HTMLDocument
}
if (Object.prototype.toString.call(obj) == "[object HTMLDocument]")
{
    // obj is a HTMLDocument
}
like image 94
annonymously Avatar answered Jan 21 '26 09:01

annonymously