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 ifobj
is of typeHTMLDocument
. 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
, andtitle
tags make an HTML Document different from PartialViews or did I miss something? – ideAvi Commented Apr 26, 2012 at 4:13
2 Answers
Reset to default 9Try 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
版权声明:本文标题:jquery - how to know if an object is an Html document using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742050321a2418030.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论