Would like to know when to use, This applies for what browser ?
if (document.all&&document.getElementById) {
// Some code block
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I don’t think there’s any significant browser left that doesn’t support
document.getElementById. Even IE6 supports it (although it gets it wrong — details — but it does the same thing withdocument.all). I’d just usedocument.getElementByIdrather thandocument.all.If you’re using this just to detect the browser, rather than specifically because you want to use
document.allordocument.getElementById, browser detection is generally a bad idea. Instead, look at doing feature detection, testing for the actual features you want. There are some great examples of feature detection here and here.Off-topic: If you’re looking to do any significant cross-browser work, you might consider using a library like jQuery, Prototype, YUI, Closure, or any of several others. They’ll smooth over browser differences for you, provide some very useful utility functions, and even (in some cases) fix browser bugs like IE’s broken handling of
document.getElementById(jQuery does that, for instance, some others may as well).