且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

加载了表示我的JS脚本的***方式

更新时间:2023-12-04 23:51:46

< script> 标签是在程序上执行的,所以这个

The javascript content of <script> tags is executed procedurally, so this

<script type="text/javascript" src="http://mysite.com/awesome.js" />
<script type="text/javascript">
    alert(Awesome.Name);
</script>

只会提醒 Awesome.Name 的内容>如果在任何之前的脚本标记中找到。

will only alert the contents of Awesome.Name if found in any previous script tag.

要了解页面上是否已完全加载所有内容,您必须使用DOMContentLoaded,对于firefox,以及ie的onreadystatechange。如果您不关心检查DOM(可能更容易),您也可以直接检查窗口对象上的加载事件。

To understand if everything has been fully loaded on the page, you have to use the DOMContentLoaded, for firefox, and "onreadystatechange" for ie. Also you can simply check the load event on the window object if you don't care about checking the DOM (could be easier).

if ( document.addEventListener ) {
    document.addEventListener( "DOMContentLoaded", function(){
         doSomething();   
    }, false );
} else if ( document.attachEvent ) { // IE        
    document.attachEvent("onreadystatechange", function(){
        if ( document.readyState === "complete" ) {
            doSomething();
        }
    });
}