Javascript Feature Detection Technology

Source: Internet
Author: User

This new technology was first developed by jquery and supported by prototype and mootools. The reason for the rise of this technology is that browser sniffing technology is not very reliable. For example, useragent is easily forged. For example, useragent of IE8 contains many browser keywords, making it difficult to identify. Some other important identification means are ineffective because browser vendors upgrade the old version and constantly promote the new version. As there are more and more types of browsers and more versions, check the core files of Ext. It is really resistant. Even Firefox and Apple's versions are listed. This will not stop expanding, therefore, feature detection technology emerged. As long as this is used to detect some features that are intelligently added by the browser factory, needless to say, Internet Explorer is the first. For example, it automatically adds tbody to the table and removes spaces for innerhtml, automatically complete the path for the href attribute ...... Although some are indeed very useful, such as adding tbody to the table can speed up reading and writing, but the standard browser does not do this, so the trouble is coming ......

Innerhtml white space removal feature. It is reasonable to say that innerhtml is developed by IE. other browsers should be rendered in full ie mode, but they always have to come up with something special. However, there is something wrong with IE's innerhtml. According to Microsoft's idea, innerhtml is used to generate DOM elements on a large scale. If some blank spaces are not enclosed in tags, they will be ignored. This is very careful. However, the standard browser considers that innerhtml generates a part of the page, and the blank space is also, so the blank space will not be ignored. Note the result of the run box. 1 indicates the element node, 3 indicates the text node, and blank indicates the text node. Jquery calls this feature leadingwhitespace.

<Br/> <! Doctype HTML> <br/> <HTML dir = "LTR" lang = "ZH-CN"> <br/> <pead> <br/> <meta charset = "UTF-8" /> <br/> <title> Feature Detection Technology by situ zhengmei </title> <br/> <SCRIPT type = "text/JavaScript"> <br/> window. onload = function () {<br/> var P = document. getelementbyid ("p"); <br/> P. innerhtml = "<span> hello! </Span> "<br/> alert (P. firstchild. nodetype ); <br/>}< br/> </SCRIPT> </P> <p> </pead> <br/> <body> <br/> <p id =" P "> </P> <br/> </body> <br/> </ptml> <br/>

RunCode

table automatically inserts tbody . In the age of table layout, this feature is very useful. If there is no tbody, the table will be displayed only when the browser parses the closed tag. If the start tag is very different from the closed tag, in other words, the table is very large and long, users cannot see anything. However, with the tbody segmentation recognition and display, we can avoid the situation where the page is blank for a long time and then all the content suddenly comes out. However, this is only because the IE rendering speed is too slow. Let's look at opera and chrome ...... This feature, jquery is called tbody, callback!

<Br/> <! Doctype HTML> <br/> <HTML dir = "LTR" lang = "ZH-CN"> <br/> <pead> <br/> <meta charset = "UTF-8" /> <br/> <meta http-equiv = "X-UA-compatible" content = "Ie = 7"> <br/> <title> Feature Detection Technology by situ zhengmei </title> <br/> <SCRIPT type = "text/JavaScript"> <br/> window. onload = function () {<br/> var DIV = document. createelement ("Div"); <br/> Div. innerhtml = "<Table> </table>" <br/> document. body. appendchild (DIV); <br/> alert (div. getelementsbytagname ("tbody "). length ,) <br/>}< br/> </SCRIPT> </P> <p> </pead> <br/> <body> </P> <p> </ body> <br/> </ptml> <br/>

Run code

invisible feature of object sub-elements . That is to say, although the rendering is successful, we can't get these things. It's a real hit. If Flash is used, modify the parameters temporarily ...... This feature is called objectall in jquery.

<br/> <! Doctype HTML> <br/> <HTML dir = "LTR" lang = "ZH-CN"> <br/> <pead> <br/> <meta charset = "UTF-8" /> <br/> <meta http-equiv = "X-UA-compatible" content = "Ie = 7"> <br/> <title> Feature Detection Technology by situ zhengmei </title> <br/> <SCRIPT type = "text/JavaScript"> <br/> window. onload = function () {<br/> var DIV = document. createelement ("Div"); <br/> Div. innerhtml = "<Object> <Param/> </Object>" <br/> document. body. appendchild (DIV); <br/> alert (div. getelementsbytagname ("object") [0]. children. length) <br/> alert (document. getelementsbytagname ("Param") [0]) <br/>}< br/> </SCRIPT> </P> <p> </pead> <br/> <body> </P> <p> </ body> <br/> </ptml> <br/>

Run code

Innerhtml: serialization of HTML strings. In general, we use innerhtml to convert a string conforming to the HTML Tag lattice to a DOM element. In this process, jquery calls it serialization, but this serialization is incomplete in IE and cannot be serialized as a link element. Therefore, do not use innerhtml to dynamically load CSS files. This feature is called htmlserialize in jquery.

<Textarea id = "runcode4" style = "width: 80%" rows = "10"> <br/> <! Doctype HTML> <br/> <HTML dir = "LTR" lang = "ZH-CN"> <br/> <pead> <br/> <meta charset = "UTF-8" /> <br/> <title> Feature Detection Technology by situ zhengmei </title> <br/> <SCRIPT type = "text/JavaScript"> <br/> window. onload = function () {<br/> var DIV = document. createelement ("Div"); <br/> Div. innerhtml = '< link href = "http://www.cnblogs.com/Skins/marvin2/brown.css" rel = "stylesheet" type = "text/CSS"/> '; <br/> document. body. appendchild (Div); <br/> alert (div. getelementsbytagname ("Link "). length) <br/>}< br/> </SCRIPT> </P> <p> </pead> <br/> <body> <br/> <H1 id =" top "> situ zhengmei's blog Ruby Louvre </p> <br/> <H3 id =" tagline "> If the webpage is not pure white, the topic of my blog has been successfully loaded! </H3> <br/> </body> <br/> </ptml> <br/> </textarea>

Run code

According to the latest findings (, 11), ie does not support serialization of link elements, but has some bugs. See the following test

<Br/> <! Doctype HTML> <br/> <HTML dir = "LTR" lang = "ZH-CN"> </P> <p> <pead> <br/> <meta charset = "UTF-8"/> <br/> <title> <br/> innerhtml bug by situ zhengmei <br/> </title> <br/> <SCRIPT type = "text/ javaScript "> <br/> window. onload = function () {<br/> var DIV = document. createelement ('div '); <br/> Div. innerhtml = '<link>'; <br/> alert (div. innerhtml); <br/> // => ''<br/> Div. innerhtml = '<div> <link> </div>'; <br/> alert (Div. innerhtml); <br/> // => '<div> </div>' <br/> Div. innerhtml = '<div> <link> </div> text'; <br/> alert (div. innerhtml); <br/> // => '<div> </div> text' <br/> Div. innerhtml = 'text <link> '<br/> alert (div. innerhtml ); <br/> // => 'text <link> '<br/>}< br/> </SCRIPT> <br/> </pead> </P> <p> <body> <br/> <H1 id = "TOP"> <br/> situ zhengmei's blog Ruby Louvre <br/> </p> <br/> <p id = "tagline"> <br/> If the webpage is not pure white, the topic of my blog has been successfully loaded! <Br/> </p> <br/> </body> </P> <p> </ptml> <br/>

Run code

Getattribute supports Style Features. IE will get an object, and others will get a string ...... As for setattribue, You can see another blog post. This feature is called a style in jquery. It is an inexplicable name!

<Br/> <! Doctype HTML> <br/> <HTML dir = "LTR" lang = "ZH-CN"> <br/> <pead> <br/> <meta charset = "UTF-8" /> <br/> <title> Feature Detection Technology by situ zhengmei </title> <br/> <SCRIPT type = "text/JavaScript"> <br/> window. onload = function () {<br/> var DIV = document. createelement ("Div"); <br/> Div. innerhtml = '<a href = "/a" style = "color: red;"> situ zhengmei </a>'; <br/> document. body. appendchild (DIV); <br/> var A = div. getelementsbytagname ("A") [0]; <br/> alert (. getattribute ("style ")) <br/>}< br/> </SCRIPT> </P> <p> </pead> <br/> <body> </P> <p> </ body> <br/> </ptml> <br/>

Run code

automatically complete the URL feature . IE will fill all the actions of SRC, href, and form of IMG into absolute paths. This bug can be solved using the second parameter of getattribute. This feature is called hrefnormalized in jquery.

<Br/> <! Doctype HTML> <br/> <HTML dir = "LTR" lang = "ZH-CN"> <br/> <pead> <br/> <meta charset = "UTF-8" /> <br/> <title> Feature Detection Technology by situ zhengmei </title> <br/> <meta http-equiv = "X-UA-compatible" content = "ie = 7 "> </P> <p> <SCRIPT type =" text/JavaScript "> <br/> window. onload = function () {</P> <p> var A = document. getelementsbytagname ("A") [0]; <br/> var IMG = document. getelementsbytagname ("IMG") [0]; <br/> var form = document. getelementsbytagname ("form") [0]; <br/> alert ("The href attribute of the link is" +. getattribute ("href") <br/> alert ("the src attribute of the image is" + IMG. getattribute ("src") <br/> alert ("the action attribute of the form is" + form. getattribute ("action ")) <br/>}< br/> </SCRIPT> </P> <p> </pead> <br/> <body> <br/> <a href =" /rubylouvre "> situ zhengmei </a> <br/> <br/> <form action ="/rubylouvre "> </form> <br/> </body> <br/> </ptml> <br/>

Run code

Dynamic parsing script features. In a standard browser, if you add a text node to the script, it will parse the content into a script, and IE will not work. It depends on its text attribute, for more information, see my other blog post "javascript dynamic parsing script". This feature is called scripteval by jquery.

<Br/> <! Doctype HTML> <br/> <HTML dir = "LTR" lang = "ZH-CN"> <br/> <pead> <br/> <meta charset = "UTF-8" /> <br/> <title> Feature Detection Technology by situ zhengmei </title> </P> <p> <SCRIPT type = "text/JavaScript"> <br/> window. onload = function () {<br/> var script = document. createelement ("script"); <br/> script. type = "text/JavaScript"; <br/> try {<br/> script. appendchild (document. createtextnode ("window. rubylouvre = 100 "); <br/>}catch (e) {}< br/> document. body. appendchild (SCRIPT); <br/> If (window. rubylouvre) {<br/> alert (window. rubylouvre) <br/> Delete window. rubylouvre; <br/>}< br/> document. body. removechild (SCRIPT ); <br/>}< br/> </SCRIPT> </P> <p> </pead> <br/> <body> </P> <p> </ body> <br/> </ptml> <br/>

Run code

Clone event featuresWhen a node is copied using the clonenode method, the events above the node are also copied. Jquery is called nocloneevent.

<Br/> <! Doctype HTML> <br/> <HTML dir = "LTR" lang = "ZH-CN"> <br/> <pead> <br/> <meta charset = "UTF-8" /> <br/> <title> Feature Detection Technology by situ zhengmei </title> <br/> <SCRIPT type = "text/JavaScript"> <br/> window. onload = function () {<br/> var addevent = (function () {<br/> If (document. addeventlistener) {<br/> return function (El, type, FN) {<br/> el. addeventlistener (type, FN, false); <br/>}; <br/>} else {<br/> return functi On (El, type, FN) {<br/> el. attachevent ('on' + type, function () {<br/> return fn. call (El, window. event); <br/>}); <br/>}< br/>}) (); <br/> var div1 = document. createelement ("Div"); <br/> div1.style.css text = "width: 200px; Height: 200px; Background: Blue"; <br/> div1.innerhtml = "Click me! "<Br/> document. body. appendchild (div1); <br/> addevent (div1, "click", function () {<br/> alert ("This Is A div1 event! ") <Br/>}); <br/> var div2 = div1.clonenode (true); <br/> div2.style. backgroundcolor = "red"; <br/> document. body. appendchild (div2 ); <br/>}< br/> </SCRIPT> </P> <p> </pead> <br/> <body> </P> <p> </ body> <br/> </ptml> <br/>

Run code

Box Model features, Ie in different rendering modes, the box model is different. In the standard box model, the total width of the element is equal to = left border + Left Border + Left fill white + width + right fill white + Right Border + right border. In Weird mode, the total width of an element is equal to the left boundary + width + right boundary. In other words, in the weird mode, the width is less. We can use offsetwidth for verification. The definition of offsetwidth can be clearly explained! Jquery is called boxmodel.

<Br/> <HTML> <br/> <pead> <br/> <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8 "/> <br/> <title> Feature Detection Technology by situ zhengmei </title> <br/> <SCRIPT type =" text/JavaScript "> <br/> window. onload = function () {<br/> var DIV = document. createelement ("Div"); <br/> Div. style. width = div. style. paddingleft = "1px"; <br/> document. body. appendchild (DIV); <br/> alert (div. offsetwidth) <br/>}< br/> </SCRIPT> </P> <p> </pead> <br/> <body> </P> <p> </ body> <br/> </ptml> <br/>

Run code

the other two cssstyles and opacity are not described in detail. There are substitutes in IE. In prototype, the browser also detects XPath, selectorsapi, element, and element. prototype support: reads and writes of innerhtml in select, Col, colgroup, frameset, HTML, title, style, table, tbody, thead, tfoot, TR, TD, and th.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.