views:

64

answers:

1

I want to determine the mimetype of a webpage.

I am specifically looking for a statement like this, which works for Firefox:

if (document.contentType == 'text/html') {  
   performX();  
} else {  
   performY();  
}

However, a statement which works across all browsers is preferred. Some google result suggested me this, but it fails:

    if (document.getElementsByTagName("head").length + document.getElementsByTagName("frameset").length != 0) {  
       performX();  
   } else {  
       performY();  
   }
A: 

You could check if there is a documentElement on the page to determine if it is a webpage.

if(document.documentElement){
      performX();
}else{
      performY();
}

That will return a HTMLCollection object so if it doesn't return one then its not there.

That is the easiest way that I can think of doing it. Hope that helps!

AutomatedTester
The problem is that all browsers seem to render a different mimetype page (say, an image page with url ending in .jpeg) as a webpage. So, unfortunately that doesn't work. Thanks for trying!
tjazz