Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

javascript - 需要获取静态文件的状态(Need to get statuses of static files)

I need to get statuses(http codes) of static files on page.

(我需要在页面上获取静态文件的状态(http代码)。)

For example html code contains

(例如html代码包含)

<link rel="stylesheet" type="text/css" href="theme.css">
<link rel="stylesheet" type="text/css" href="https://example.com/theme.css">
<script src="script.js"></script>
<script src="https://example.com/script.js"></script>
<img src="image.png">
<img src="https://example.com/image.png">

On output need to get json like this Only 404 needed.

(在输出上需要像这样获取json仅需要404。)

{
  "css": [
    {
      "https://example.com/theme.css": "404",
      "https://example2.com/theme.css": "404"
    }
  ],
    "js": [
    {
      "https://example1.com/script.js": "404",
      "https://example.com/script.js": "404"
    }
  ],
      "images": [
    {
      "https://example.com/image.png": "404",
      "https://example.com/image.png": "404"
    }
  ]
}

It must be done with only Javascript.

(必须仅使用Javascript完成。)

I am going to use this

(我要用这个)

var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
        callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);

It is necessary that upon receiving the status, this should not be done by executing a separate GET request to the file.

(有必要在收到状态后,不要通过对文件执行单独的GET请求来完成此操作。)

But I can't find the way how to do this without executing a separate GET request.

(但是我找不到执行单独的GET请求的方法。)

Who can find the solution?

(谁可以找到解决方案?)

  ask by XueQing translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you are using Server side languages like PHP then i have some idea.

(如果您使用的是PHP等服务器端语言,那么我有一些想法。)

eg.

(例如。)

<?php
$url= $_POST["url"];
$curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => $url, CURLOPT_HEADER => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_NOBODY => true)); $header = explode("
", curl_exec($curl)); curl_close($curl); $string = $header[0];
if(strpos($string, "200")){
    echo "200";
}else{
    echo "404";
}
?>

Call the above file in AJAX with url parameter.

(使用url参数在AJAX中调用上述文件。)

Or write similar lines of in your server side.

(或在服务器端写类似的行。)

Like (python):

(像(python):)

s=socket.socket()
s.connect('url',80)
s.send('GET /path HTTP/1.0
Host:hostname

')
print s.recv(1024) #some lines of headers

From the above, in short, it is not possible to get status code of file present in any host without sending request .

(综上所述,简而言之,如果不发送request ,就不可能获取任何主机中存在的文件的status code 。)

Hence, it is not possible without sending multiple AJAX request (seperate GET requests)

(因此,不发送多个AJAX请求(单独的GET请求)是不可能的)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...