简体   繁体   English

计算javascript中的http请求数

[英]Counting the number of http requests in javascript

Is there a way in javascript to count the number of http request that a website does to a server? 有没有办法在javascript中计算网站对服务器的http请求数? EXAMPLE: In my local webserver running with apache tomcat i have a simple html page helloworld.html with a style.css file, so when i type "localhost:8080/helloworld.html" the browser does two requests, one for helloworld.html and one for style.css. 示例:在使用apache tomcat运行的本地Web服务器中,我有一个带有style.css文件的简单html页面helloworld.html,因此当我输入“localhost:8080 / helloworld.html”时,浏览器会执行两个请求,一个用于helloworld.html和一个用于style.css。

My goal is to show this number on helloworld.html for a video element, when i play it start several number of http get to the webserver. 我的目标是在helloworld.html上显示一个视频元素的这个数字,当我播放它时,启动几个http到网络服务器。

I'm trying for several days on the web but I have not found anything. 我在网上试了好几​​天但我还没找到任何东西。 I found webrequest api but i think that only chrome extension can use it. 我找到了webrequest api,但我认为只有chrome扩展可以使用它。 I found also chrome.devtools.network API but i can use it only when chrome dev tools is opened. 我还找到了chrome.devtools.network API但我只能在打开chrome dev工具时使用它。

With window.performance API provided by HTML5, there is an easy option to find out the total number of external resources loaded by the page. 使用HTML5提供的window.performance API,可以轻松找到页面加载的外部资源总数。

var totalNoOfResrouces = window.performance.getEntriesByType("resource").length;

There is no easy option like finding the individual details like images, css, scripts, etc., But still you can find those if all your resources URL are in some proper format like http://example.com/img/sample.jpg or http://example.com/scripts/app.js or http://example.com/styles/master.css 没有简单的选择,例如查找图像,CSS,脚本等个别详细信息,但如果您的所有资源URL都采用某种正确的格式,例如http://example.com/img/sample.jpg,您仍然可以找到这些选项。或http://example.com/scripts/app.jshttp://example.com/styles/master.css

_.filter(window.performance.getEntriesByType("resource"),function(a){ return a.name.indexOf("/img/") > 0 });

note: using _ for filtering. 注意:使用_进行过滤。 you can use without _. 你可以不用_。 This method also have its own caveats like that it will return only successful requests. 这种方法也有自己的注意事项,它只返回成功的请求。 Pending resources won't be listed 待定资源将不会列出

You can count the <link> and <script> manually (for a simple page without ajax). 您可以手动计算<link><script> (对于没有ajax的简单页面)。

With JQuery: 使用JQuery:

var requestsNumber = 1; // The document itself.

$('link').each(function() {
    var href = $(this).attr('href');

    if (href && -1 === href.indexOf('http://')) {
        requestNumber++;
    }
});

$('script').each(function() {
    var src = $(this).attr('src');

    if (src && -1 === src.indexOf('http://')) {
        requestNumber++;
    }
});

This suppose you correctly use relative url. 这假设您正确使用相对URL。 This won't give you any state for the responses (like 404 for instance). 这不会给你任何响应状态(比如404)。 Note that indexOf is not working on IE8. 请注意, indexOf不适用于IE8。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM