`

页面加载时间判断

阅读更多
  javascript计算页面加载时间,代码如下:

view source
print?
<html><head>
<title>Calculating Page Loading Time</title>
<SCRIPT LANGUAGE="JavaScript">
//calculate the time before calling the function in window.onload
beforeload = (new Date()).getTime();
function pageloadingtime()
{

     //calculate the current time in afterload
    afterload = (new Date()).getTime();
     // now use the beforeload and afterload to calculate the seconds
    secondes = (afterload-beforeload)/1000;
     // If necessary update in window.status
    window.status='You Page Load took  ' + secondes + ' seconde(s).';
     // Place the seconds in the innerHTML to show the results
    document.getElementById("loadingtime").innerHTML = "<font color='red'>(You Page Load took " + secondes + " seconde(s).)</font>";
    
}
  
window.onload = pageloadingtime;
</SCRIPT>
</head>
<body >
<table border width=40%>
<tr><td colspan=2><strong>Page Loading Script</strong></td></tr>
<TR>                 <TH> sample </TH> <TH> test</TR>
<TR ALIGN="left">    <TH> Hi  1<BR>bloop </TH> <TH> Head 2 </TR>
<TR VALIGN="bottom"> <TH> test 1<BR>bloop </TH> <TH> Head 2 </TR>
<TR>                 <TH> hi 1<BR>bloop </TH> <TH> Head 2 </TR>
<TR ALIGN="center">  <TD> y<BR>aaaa     </TD> <TD> 4.23   </TR>
<TR ALIGN="right">   <TD> new     </TD> <TD> test</TR>
</table>
<p><div id="loadingtime"></div></p>
</body>
</html>




由于window.onload()在所有页面元素加载完成后执行其函数,所以如果页面元素中存在如过大图片,页面加载速度就有所影响,因此,急需替代函数,如果不用其他js框架,如何解决,后来一搜,发现有人已经解决这事情了。http://www.brothercake.com/site/resources/scripts/domready/

window.onload()的替代函数(不使用JS库)

    博客分类:
    HTML & W3C & DOM

SafariCC++C#XHTML
由于window.onload()在所有页面元素加载完成后执行其函数,所以如果页面元素中存在如过大图片,页面加载速度就有所影响,因此,急需替代函数,如果不用其他js框架,如何解决,后来一搜,发现有人已经解决这事情了。http://www.brothercake.com/site/resources/scripts/domready/。

相当的不错。其源码:

domFunction.js
Js代码  收藏代码

    // DF1.1 :: domFunction 
    // ***************************************************** 
    // DOM scripting by brothercake -- http://www.brothercake.com/ 
    // GNU Lesser General Public License -- http://www.gnu.org/licenses/lgpl.html 
    //****************************************************** 
     
     
    //DOM-ready watcher 
    function domFunction(f, a) 
    { 
    //initialise the counter 
    var n = 0; 
     
    //start the timer 
    var t = setInterval(function() 
    { 
       //continue flag indicates whether to continue to the next iteration 
       //assume that we are going unless specified otherwise 
       var c = true; 
     
       //increase the counter 
       n++; 
     
       //if DOM methods are supported, and the body element exists 
       //(using a double-check including document.body, for the benefit of older moz builds [eg ns7.1] 
       //in which getElementsByTagName('body')[0] is undefined, unless this script is in the body section) 
       if(typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null)) 
       { 
        //set the continue flag to false 
        //because other things being equal, we're not going to continue 
        c = false; 
     
        //but ... if the arguments object is there 
        if(typeof a == 'object') 
        { 
         //iterate through the object 
         for(var i in a) 
         { 
          //if its value is "id" and the element with the given ID doesn't exist 
          //or its value is "tag" and the specified collection has no members 
          if 
          ( 
           (a[i] == 'id' && document.getElementById(i) == null) 
           || 
           (a[i] == 'tag' && document.getElementsByTagName(i).length < 1) 
          ) 
          { 
           //set the continue flag back to true 
           //because a specific element or collection doesn't exist 
           c = true; 
     
           //no need to finish this loop 
           break; 
          } 
         } 
        } 
     
        //if we're not continuing 
        //we can call the argument function and clear the timer 
        if(!c) { f(); clearInterval(t); } 
       } 
       
       //if the timer has reached 60 (so timeout after 15 seconds) 
       //in practise, I've never seen this take longer than 7 iterations [in kde 3 
       //in second place was IE6, which takes 2 or 3 iterations roughly 5% of the time] 
       if(n >= 60) 
       { 
        //clear the timer 
        clearInterval(t); 
       } 
       
    }, 250); 
    }; 



页面使用:
Html代码  收藏代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
     
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 
     
    <head> 
     
    <title>domFunction</title> 
     
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
     
    <!-- domFunction constructor by brothercake - http://www.brothercake.com/ --> 
    <script type="text/javascript" src="domFunction.js"></script> 
    <script type="text/javascript"> 
     
     
    /* 
    function myFunction() 
    { 
       alert("The DOM is ready"); 
    }; 
    var foobar = new domFunction(myFunction, { 'h1' : 'tag'}); 
    */ 
     
     
    var foobar = new domFunction(function() 
    { 
       alert("The DOM is ready"); 
     
    }, { 'h1' : 'tag' }); 
     
     
    </script> 
     
    </head> 
     
    <body> 
     
    <h1>domFunction</h1> 
     
    </body> 
    </html> 


很容易理解,作者还有一个简易版本,不过不能检查到某个特定元素,叫domReady.js
Js代码  收藏代码

    // DR1.0 :: domReady 
    // ***************************************************** 
    // DOM scripting by brothercake -- http://www.brothercake.com/ 
    // GNU Lesser General Public License -- http://www.gnu.org/licenses/lgpl.html 
    //****************************************************** 
     
    //DOM-ready watcher 
    function domReady() 
    { 
    //start or increment the counter 
    this.n = typeof this.n == 'undefined' ? 0 : this.n + 1; 
     
    //if DOM methods are supported, and the body element exists 
    //(using a double-check including document.body, for the benefit of older moz builds [eg ns7.1] 
    //in which getElementsByTagName('body')[0] is undefined, unless this script is in the body section) 
    //>>> and any elements the script is going to manipulate exist 
    if 
    ( 
       typeof document.getElementsByTagName != 'undefined' 
       && (document.getElementsByTagName('body')[0] != null || document.body != null) 
       //>>> && document.getElementById('something') != null 
    ) 
    { 
    //>>>-- DOM SCRIPTING GOES HERE --<<< 
     
     
       alert("The DOM is ready!"); 
     
     
    //>>>-----------------------------<<< 
    } 
     
    //otherwise if we haven't reached 60 (so timeout after 15 seconds) 
    //in practise, I've never seen this take longer than 7 iterations [in kde 3.2.2 
    //in second place was IE6, which takes 2 or 3 iterations roughly 5% of the time] 
    else if(this.n < 60) 
    { 
       //restart the watcher 
       //using the syntax ('domReady()', n) rather than (domReady, n) 
       //because the latter doesn't work in Safari 1.0 
       setTimeout('domReady()', 250); 
    } 
    }; 
    //start the watcher 
    domReady(); 


页面使用:
Html代码  收藏代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
     
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 
     
    <head> 
     
    <title>domReady</title> 
     
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
     
    <script type="text/javascript" src="domReady.js"></script> 
     
    </head> 
     
    <body> 
     
     
     
    </body> 
    </html> 
分享到:
评论

相关推荐

    C#判断webbrowser页面最终加载完成

    5. **超时机制**:设定一个合理的超时时间,如果在此时间内没有其他条件满足,就假设页面已经加载完成。这种方法用于处理那些可能出现无限加载或者响应缓慢的页面。 综上所述,要判断WebBrowser控件的页面是否最终...

    动态加载Css 浏览器判断

    在现代Web开发中,动态加载CSS(层叠样式表)是一种常见的优化策略,它能够提高网页的性能,减少页面初始化时的负担。浏览器判断则是一个关键的辅助技术,用于确保我们的代码能够在不同的浏览器环境中正常运行。这...

    基于时间知觉的移动APP页面加载方式研究.pdf

    同时,对于这两种加载方式,用户的时距估计普遍低于实际的加载时间。这表明,非模态加载和隐藏等待加载能够有效缩短用户感知到的等待时间。 非模态加载允许用户在页面加载过程中继续执行其他操作,减少了用户注意力...

    页面加载时 提示 正在加载中

    当用户点击链接或提交表单时,页面加载的过程可能会消耗一些时间,特别是在处理大量数据或者进行复杂的计算时。这种等待可能会让用户感到不耐烦,甚至误以为页面崩溃了。因此,"正在加载中"的提示信息可以有效地告诉...

    javascript页面加载完执行事件代码

    这样可以减少页面加载时间,提升用户体验。 在实现页面加载完毕执行代码的场景中,需要注意以下几点: 1. 确保在DOM元素生成之后执行相关的JavaScript代码,避免在DOM元素还未加载完成时就尝试访问它们,这可能会...

    js实现页面加载完毕之前loading提示效果

    JavaScript是一种强大的客户端脚本语言,常用于...这种技术对于优化网页性能和用户体验至关重要,尤其是对于那些内容加载时间较长的网站。同时,也可以根据实际需求调整提示文本和样式,以更好地匹配网站的整体设计。

    手机页面预加载效果

    这在资源有限的移动设备上尤其有用,可以减少初始加载时间,提升页面响应速度。 3. **资源优先级管理**:通过判断哪些资源对用户体验最关键,优先加载这些内容,比如页面布局和主要功能,然后逐步加载非必需的图片...

    延迟加载js代码下载(包括页面延迟加载以及图片延迟加载)

    - 图片尺寸优化:对图片进行压缩和适配不同设备尺寸,减少加载时间。 - 使用WebP或AVIF等高效图片格式:这些格式的图片体积更小,但视觉效果不打折。 综上所述,延迟加载是提升网页性能的有效手段,通过JavaScript...

    js页面加载进度条

    JavaScript(简称JS)页面加载进度条是一种用户界面交互设计,用于提供可视化的页面加载状态,让用户了解网页内容的加载进度,提升用户体验。这种效果通常在大型应用或包含大量资源的网页中常见,因为它能平息用户的...

    页面加载效果

    页面加载效果是网页设计中一个重要的用户体验组成部分,它在用户打开网页时呈现过渡动画,以掩盖内容实际加载的时间,给予用户一种更为流畅的浏览体验。"fakeloader"是一种常见的页面加载效果实现方式,它通常由...

    Jquery网页部分内容加载

    在网页设计和开发中,有效地管理页面加载是优化用户体验的关键因素。"Jquery网页部分内容加载"这个主题就涉及到了如何利用jQuery库实现网页内容的按需加载,即延迟加载(Lazy Loading)或局部加载(Partial Loading...

    页面内容滚动加载

    页面内容滚动加载是一种常见的网页优化技术,也被称为“无限滚动”或“滚动侦测加载”。这种技术允许网页在用户滚动到页面底部时自动加载更多内容,而不是一次性加载所有内容,从而提高了网页的加载速度和用户体验。...

    实用网页加载等待效果+带60多款GIF动画效果

    网页加载等待效果是用户体验设计中的一个重要环节,尤其是在大型或复杂网站中,用户在等待页面完全加载时可能会感到不耐烦。这种效果通过显示动画或动态图标来告知用户系统正在处理请求,提供一种视觉反馈,从而改善...

    网页图片随滚动条加载

    总结起来,网页图片随滚动条加载是提升网页性能、优化用户体验的有效手段,利用AJAX和异步加载技术,可以有效地减少页面初始化的加载时间,提高网页的响应速度,特别是在图片资源丰富的网页上,效果尤为明显。

    jquery图片延迟加载

    这样可以显著减少初始页面加载时间,提升网页的响应速度,同时也节省了用户的网络流量。 **二、jQuery图片延迟加载的优势** 1. **提高页面加载速度**:只加载可见区域的图片,减轻服务器压力,加快页面初次渲染的...

    网页图片延迟加载插件

    这种技术可以显著减少网页的初始加载时间,提高用户体验,尤其是对于图片丰富的网站来说更为重要。 在网页开发中,常见的实现图片延迟加载的方式有以下几种: 1. **Intersection Observer API**:这是现代浏览器...

    推荐移动端网页使用的图片懒加载

    - **提高页面加载速度**:只加载当前需要的图片,减少首屏加载时间。 - **节省流量**:对用户来说,只有看到的图片才会消耗流量。 - **优化用户体验**:减少页面加载对CPU和内存的压力,使得页面操作更流畅。 2....

    JQuery实战视频教程_页面加载时弹出一个对话框

    在本篇jQuery实战视频教程中,我们聚焦于一个常见的网页交互功能:页面加载时自动弹出对话框。这个功能在很多网站中都有应用,比如欢迎提示、重要信息通知或者用户确认操作等。jQuery库因其简洁易用的API,使得实现...

    scrollLoad_MingGe2.54滚动加载插件 懒加载

    这样可以显著减少初始页面加载时间,使用户能够更快地看到页面的主要内容,而不是等待所有资源完全加载完毕。在 MingGe2.54 插件中,这一机制被巧妙地应用于图片、视频和其他需要延迟加载的元素上。 在IE6、IE7、IE...

    简单的网页延迟加载效果

    这样可以显著减少初始加载时间,提升用户体验,特别是对于内容丰富的长页面或多媒体密集型网站。 在"简单的网页延迟加载效果"项目中,我们可以看到几个关键的文件: 1. `grey.gif`:这是一个通常用于替换原始图片...

Global site tag (gtag.js) - Google Analytics