admin管理员组

文章数量:1319025

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!

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!

Share Improve this question edited Apr 26, 2012 at 3:46 ideAvi asked Apr 26, 2012 at 3:39 ideAviideAvi 1392 gold badges3 silver badges13 bronze badges 7
  • An HTML document doesn't necessarily have to have a title tag. – Brad Commented Apr 26, 2012 at 3:42
  • 1 Look at this question and its answers. – annonymously Commented Apr 26, 2012 at 3:44
  • @annonymously: obj may also be a PartialView and it must take a different function than HTMLDocuments'. How would it be? – ideAvi Commented Apr 26, 2012 at 4:02
  • Use if (obj instanceof HTMLDocument) to find out if obj is of type HTMLDocument. From what I understand, that's what you want. – annonymously Commented Apr 26, 2012 at 4:12
  • @Brad: Yup. that's why I've included title tags in all of my HTML Documents ;) only html, head, and title tags make an HTML Document different from PartialViews or did I miss something? – ideAvi Commented Apr 26, 2012 at 4:13
 |  Show 2 more ments

2 Answers 2

Reset to default 9

Try this:

if (obj instanceof HTMLDocument)
{
    // obj is a HTMLDocument
}
if (Object.prototype.toString.call(obj) == "[object HTMLDocument]")
{
    // obj is a HTMLDocument
}

You can try this

$obj.is('html')

本文标签: jqueryhow to know if an object is an Html document using javascriptStack Overflow