The different browsers to link tag Onload/onreadystatechange event support Difference Analysis _javascript skill

Source: Internet
Author: User
1,onload Events
Copy Code code as follows:

<! DOCTYPE html>
<HTML>
<HEAD>
<meta charset= "Utf-8"/>
<title>link Element onload</title>
<link type= "Text/css" rel= "stylesheet" href= "Http://i3.sinaimg.cn/rny/webface/login/css/login101021_min.css" onload= "alert (this)"/>
</HEAD>
<BODY>
</BODY>
</HTML>

IE6/7:

IE8/9:

Opera:

That is, Ie6/7/8/9/opera support the OnLoad event, Firefox/safari/chrome not supported.

Note: Create link tag with JS and add it to head, as above.
2,onreadystatechange Events

Copy Code code as follows:

<! DOCTYPE html>
<HTML>
<HEAD>
<meta charset= "Utf-8"/>
<title>link Element onreadystatechange</title>
<link type= "Text/css" rel= "stylesheet" href= "Http://i3.sinaimg.cn/rny/webface/login/css/login101021_min.css" Onreadystatechange= "alert (this)"/>
</HEAD>
<BODY>
</BODY>
</HTML>

IE6/7/8/9 was shot two times and no other browsers were playing. Indicates that only IE supports the onreadystatechange event of the link element. Pop up two times respectively is readystate for Loading,complete state. ReadyState can be used to determine loading conditions. We then use JS dynamic Create a LINK element to try,
Copy Code code as follows:

<! DOCTYPE html>
<HTML>
<HEAD>
<meta charset= "Utf-8"/>
<title>link Element onreadystatechange</title>
</HEAD>
<BODY>
<script>
function Createel (type, attrs) {
var el = document.createelement (type),
attr
For (attr in attrs) {
if (Attrs.hasownproperty (attr)) {
El.setattribute (attr, attrs[attr]);
}
}
Return el;
}
var link = createel (' link ', {
HREF: ' Http://i3.sinaimg.cn/rny/webface/login/css/login101021_min.css ',
Rel: ' stylesheet ',
Type: ' Text/css '
});
Link.onreadystatechange = function () {
Alert (This)
}
document.getElementsByTagName (' head ') [0].appendchild (link);
</script>
</BODY>
</HTML>

The Ie6/7/8/9 still pops up 2 times. Firefox/safari/chrome still hasn't played. Seems to be all right, but the magic is this time in opera popped out once, explain opera support dynamically create link element when the onReadyStateChange event.

Related:

Https://developer.mozilla.org/en/HTML/Element/link
Http://msdn.microsoft.com/en-us/library/ms535848%28v=VS.85%29.aspx
Http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html

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.