This question can be answered easily with “object detection” – why? Because some obscure browsers won’t be treated correctly and browsers that appear after you’ve written the page may not be adequately covered either as a result you will end up with some error in some browsers. Object detection works fairly in all browsers.
But, certain features of Javascript don’t work in all browsers. As an example “innerText” property doesn’t work in Firefox but in IE, Chrome Etc…this problem could be solved easily like the following ..
var text = x.innerText || x.textContent //innerText for IE or other browsers and textContent for Firefox that works well.
Now, consider the following statement
Google Chrome doesn’t read encoding information that’s declared with document.write(). If you’re using this method to declare encoding in iframes, for example, you may see garbled characters when the iframe is rendered. Instead of:
document.write("<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">"); ... other JavaScript code ... </meta>
and when it might arrived? Let’s say you are using a Rich Text Editor where you will have onload events with document.write() and you will notice that iframe attribute (e.g name) value is not getting in the browser. Rather Chrome is setting name attrribute value from itself.
There is a tricky way to get it solved, in my case I used Firebug extension to get the attribute value and then detect the browser. Well in that case object detection doesn’t work for me. I must use browser detection right.
Let’s have a look at the following codes to get some idea about what I have been talking…
function notEmpty(){ var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1; var textvalue = ''; var framename = ''; var elementValue = ''; if(is_chrome) { elementValue = window.frames['Rich text editor'].document.body.innerText; } else if(navigator.appName =="Microsoft Internet Explorer") { elementValue = window.frames['myIframe'].document.body.innterText; } else { elementValue = window.frames['myIframe'].document.body.textContent; } if(elementValue =='') { //do what you want return false; } return true; }
Conclution: It depends, generally it’s always good to use object detection no doubt. But when you have no option? Well then don’t wait to use browser detection eh…






