Reputation: 278
I need to get statuses(http codes) of static files on page.
For example html code contains
<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.
{
"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.
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.
But I can't find the way how to do this without executing a separate GET request.
Who can find the solution?
Upvotes: 2
Views: 388
Reputation: 800
I need to make some assumptions here, so please check, whether those are true :-)
I'm afraid, you won't get much around making additional requests.
Here are some ideas, you could further investigate:
Would be great if you could provide more details on what you are trying to do.
Cheers
Ryuno-Ki
Upvotes: 0
Reputation: 4005
If you are using Server side languages like PHP then i have some idea. 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("\n", 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.
Or write similar lines of in your server side. Like (python):
s=socket.socket()
s.connect('url',80)
s.send('GET /path HTTP/1.0\r\nHost:hostname\r\n\r\n')
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
.
Hence, it is not possible without sending multiple AJAX request (seperate GET requests)
Upvotes: 2