简体   繁体   中英

Changing if a div's class if unable to ping a url. (Test if a site is down)

I have been researching this for a while. For the site static.etreeblog.com I would like to change a duv's class if a website is offline.

Ways I have researched:

-Using the onerror tag with an image to run a function. -Problem: One of the sites i want to test uses an external image host, meaning even if the site was offline the image could be.

Example of what I want to do:

function checksite( site, url ){
if url == online {
document.getElementByClass('site').setClass('online')
}
}

checksite('site1', 'www.example.com')
checksite('site2, 'www.example2.com')

Thankyou in advance.

Extra information: -The divs will be set to offline by default. -I am using tumblr to host the status tester (If that helps).

Use PHP, Its a solid way to do this.

Host this script on your server (file name say test.php )

<?php 
$url = $_REQUEST["url"];
$port = $_REQUESR["port"]; // Or make it default
$fp = fsockopen($url, $port, $errno, $errstr, 0.4); //(line 47)
if (!$fp) {
  echo "OFFLINE";
}
else{
 echo "ONLINE";
}
?>

Now loop AJAX to open this file and check whether it is echoing ONLINE or OFFLINE.

You will need help on the server side. You could do an AJAX call to your server and it will call the url and return with the site status. If it is that server your monitoring then the AJAX error code will tell you the status.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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