开发者

Get page's content type via the DOM

Is there a way to get the current page's content/mime type fr开发者_C百科om the DOM?


Firefox (or any other Gecko based web browser) has the document.contentType attribute.

To do it in a standards-based way you'd have to check the headers and see if there is a meta tag with attribute http-equiv="content-type" and interpret its content. Otherwise you're out of luck.


If the <meta http-equiv="content-type" content="..."> tag is set, you can get its value with this Javascript:

var cucc=document.querySelector('meta[http-equiv="content-type"]');
alert(cucc.getAttribute('content'));

jsFiddle Demo

Should work in any modern browser and in IE8 (standards mode) as well.


Firefox supports document.contentType which is non-standard but reads the content-type from the HTTP headers. (Firefox and Gecko only. See here) IE will need some hacking I think.


In Firefox, document has the property contentType, but as far as I'm aware it's not supported in any other browser.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜