当页面完全加载完成后执行一个JS函数

方法1.如下程序,当页面完全加载后执行openTheIndexPage()方法  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
<title>Telecommunications Data Collection System</title>  
<script type="text/javascript" src="<%=contextPath%>/js/baseframe.js"></script>  
<script type="text/javascript" src="<%=contextPath%>/js/cookies.js"></script>  
<script type="text/javascript" src="<%=contextPath%>/js/tag/tag.js"></script>  
<script language="javascript" for="window" event="onload">   
    function openTheIndexPage() {       
        openMyURIWithCid(true, 'root', 'IDX', "iframe/dispatch.jsp?url=tdc/zhk/impctrlobjinf/index/index.jsp", '首页',  
                'top.tagmenu', 'top.maintop', true,  
                'system/accessPaths.do?currentModuleCode=IDX',  
                'mainmenu', true);  
    };  
    if(document.readyState=="complete"){  
        openTheIndexPage();  
    }   
</script>  
</head>   
<body>   
</body>      
</html>  
方法2:可以是以下几种,但是效果不如方法1.   
<body onload="function name()"> </body >   
<script>window.onload=function name </script>   
<script language="javascript" for="window" event="onload">function name(); </script>   
第二种只能写入一个函数,而且无法给变量,其中最好用的是最后一种,可以独立写出来,怎么写都行。  
方法3:<body onload="xxx()"> </body> xxx()为你要执行的函数  
方法4:在script标记里加defer   
即 <script defer="defer" language="javascript">   
或者 <script defer language="javascript">   
在整个页面加载完后运行脚本。(没有效果)  

猜你喜欢

转载自www.cnblogs.com/nepulgh/p/9033383.html