Javascript_DOMContentLoaded&nbsp…

Syntax

addEventListener Method object.addEventListener("DOMContentLoaded", handler, useCapture)

Standards information

Event information

Synchronous No
Bubbles Yes
Cancelable No

Event handler parameters

pEvtObj [in]

Type: Event

Remarks

The DOMContentLoaded event fires when the markup of a webpage has been parsed, which means it also fires before the onload event.

DOMContentLoaded is a good place to perform initialization tasks for your webpage, such as registering event handlers, initializing handles to support objects, and so on. This allows your initialization tasks to occur while the remaining resources for the webpage are being downloaded.

For more information, see the DOMContentLoaded test drive demo.

Note  The DOMContentLoaded event is supported only for webpages displayed using IE9 Standards mode or higher.

Examples



<!doctype html> 

<html> 

<head> 

<script type="text/javascript"> 



 function logMsg( sMsg ) { 

      var oLog = document.getElementByIdx_x('ResultLog'); 

      oLog.innerText += "\n" + sMsg; 

 } 



 function addListener(obj, eventName, listener) { 

      if( obj.addEventListener ) { 

             obj.addEventListener( eventName, listener, false ); 

      } else

             obj.attachEvent("on" + eventName, listener); 

      }

 }

 function handleDCL( e ) { 

    logMsg( "DOMContentLoaded event fired.\n" ); 

    var o = document.getElementByIdx_x('button1');

    addListener(o, "click", handleClick1); 

    } 

 function handleClick1( e ) { 

 logMsg( "Button 1 clicked.\n" ); 

 } 

 if(!window.addEventListener) {

         logMsg( "Can't add eventListeners; not supported."

   } else

        addListener(document, "DOMContentLoaded", handleDCL);               

        addListener(window, "load", handleLoad); 

</script> 



</head> 

<body> 



<button id="button1">Click me</button> 

 <div> 

  <p id="ResultLog">Results appear here </p> 

</div> 

</body> 

</html>

See also
document
onload

 http://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/hh869434.aspx

發佈了76 篇原創文章 · 獲贊 4 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章