document.onreadystatechange = subSomething;//當頁面加載狀態(tài)改變的時候執(zhí)行這個方法. function subSomething() { if(document.readyState == "complete"){ //當頁面加載狀態(tài)為完全結束時進入 //你要做的操作。 } }
<script type="text/javascript"> var xmlHttp; //創(chuàng)建一個XmlHttpRequeset對象 function createXMLHttpRequest()...{ if(window.ActiveXObject)...{ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } else if(window.XMLHttpRequest)...{ xmlHttp = new XMLHttpRequest(); } } //開始一個請求 function startRequest()...{ createXMLHttpRequest(); xmlHttp.onreadystatechange = handlestatechange; xmlHttp.open("GET", "SimpleRespose.xml", true); xmlHttp.Send(null); } function handlestatechange()...{ if(xmlHttp.readyState == 4)...{//描述一種"已加載"狀態(tài);此時,響應已經(jīng)被完全接收。 if(xmlHttp.status == 200)...{//200表示成功收到 alert("The Server Replied with:" + xmlHttp.responseText) } } } </script>
<!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 :
即IE6/7/8/9/Opera都支持onload事件, Firefox/Safari/Chrome不支持。
注:用JS創(chuàng)建link標簽再添加到head中,情況如上。
2,onreadystatechange事件
<!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>
<!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>
如對本文有疑問,請?zhí)峤坏浇涣髡搲瑥V大熱心網(wǎng)友會為你解答?。?點擊進入論壇