The method to determine the existence of the object
posted November 2, 2008 - 3:57amOriginal: http://www.yesave.com/?p=27
Following is a translation of the original text:
The method to determine the existence of the object
Very soon you will note that, JavaScript function in some parts of the browser is invalid. If you want to use some of the advanced features of the script, you should first check whether the browser to support the use of the object, this article specifically to determine the right way.
Through the judge’s version of the browser: No!
If you want to know whether the browser code to support the use of those objects, remember, are not always through the browser to determine the version. I’m sure you know some of you to support the browser code, and some browsers do not support your code, but you have to consider what other browsers? Those unknown small browser?
Even if you can detect 90% of the users of the browser and version, there are some well-known browser can not be right to run your code, the result is either a lot of unusual information, or some of the script is not correct执行. In either case, you are using loopholes in the code to deceive the end user of the Web site.
Case Study: mouseovers
An old case can be confirmed. Although this situation does not exist now, but the same principle or examples exist.
A generally accepted fact is that IE 3 does not support the document.images array, but the array of mouseover script is also extremely important. Therefore, we should ban on mouseover script in IE 3 in the implementation of the browser. One solution to the browser to judge, to determine when a user’s browser is IE 3, not the implementation of this function.
However, most of the operating system, Netscape 2 browser does not support the same array document.images. If you only judge the browser is IE 3, then use the Netscape 2 users will see a lot of unusual information.
Why, then, together with Netscape 2 does not detect it together? Because even if nothing is done to make up.
In the run OS / 2 on the Netscape 2 and is fully compatible with Netscape 3, and can handle the mouseover effects. Nevertheless, we often do not see why would the result be achieved? Because web developers to use the browser detection means, in the mouseover script in shielding the browser Netscape 2. Therefore, developers, in the absence of sufficient reason to deprive the user has a good interactive experience. Suitable object detection can be avoided this situation from happening.
Finally, a growing number of browser allows users to the browser authentication string to modify their favorite content, so that there is great possibility of real users can not detect the use of the browser version, as well as, naturally can not be guaranteed Trouble-free operation of the code. From this, web developers once again deprived of the additional user interaction effect. To make matters worse, such a code is usually written by the poor.
Since the browser version is not reliable, then the JavaScript version is more credible now?
Through the judge’s version of JavaScript: No!
Planning for the first time, Netscape is fully aware of the future of the browser will support much more than it is now the target, and web developers new and old must be able to distinguish between the browser.
The initial plan is to allow developers of the browser version of the judge. For example, only certain browsers support JavaScript 1.x, and so on. In the script to add labels version attributes, so that if the browser does not support the corresponding version of JavaScript, the implementation of this nature would not have script.
However, when Microsoft set foot in the browser market, the idea will not be able to carry out any longer. Despite early as Netscape 4 and IE 4 when support JavaScript 1.2, but even more imaginative people who believe that they will not support the same JavaScript 1.2. For this version, already out of date, and certainly irrelevant and target detection.
So do not use the JavaScript version number to do what they do not produce any practical results.
The right approach: determine the target
On the contrary, we only need a simple way to determine whether the browser to support the use of the object (or method, or an array of properties). We use this example mouseover. This script relies on the document.images array, so of course the most important thing is to determine whether the browser to support him, the following is a specific way:
if (document.images)
(
do something with the images array
)
Now you have a complete security, to run the code of the browser will certainly support this script. Document.image conditional statements to determine the existence of an array, if the return to true, then this script will be executed, on the contrary, if this array does not exist, it will return false, but this script will not be executed.
There is also a commonly used testing window.focus for the. This is a method (a JavaScript to tell you what to do to order). If we want to use this method, we must first detect whether the browser supports this approach.
Detection function of the existence of the correct method is as follows, bearing in mind do not function in the back of increases in brackets:
if (window.focus)
Above this is the meaning of the code: “The browser is window.focus support this function” and the following code different meaning:
if (window.focus ())
This code is the focus of the judging results, and have assumed that the browser supports the focus of the method, if not support, which will be reported when the abnormal. Added in parentheses after the fact, the implementation of the function, and this is not what we want. Therefore, the detection time and will not increase brackets, and only after the detection through the implementation of this function together with brackets. For example, the following examples:
if (window.focus) window.focus ()
Focus
Over all the discussion is focused on: JavaScript, if you want to use the document.images, first of all to judge whether to support the document.images. If you want to use window.focus method, first determine whether the browser whether or not to support this approach.
If you always use the object before the test, you will not have a script similar to the exceptions, to pay some of the code is only part of the function of the browser has been screening it out.
Translator’s Note:
Any war will bring a lot of side effects, described in this article is mainly taken place in the browser wars of the time, just as the Cold War, resulting in a lot left over. However, ecma-262 standards, so that this situation has been eased a little, but ecma-262 in the third edition of the clearly defined, allowing each to its own expansion, the result of the expansion is not compatible with the natural, natural to use this article The way to judge. Even if we do not have to determine all the targets, if a browser that support ecma-262 standard, at least we know what objects do not have to judge, it is a kind of consolation.
Website: http://www.yesave.com/

Comments
Post new comment