Check if the html page exists HTML / Javascript [closed]

-4

How do I click the link automatically to check if the page is online? No php.

    
asked by anonymous 24.08.2017 / 20:07

3 answers

10

If the website you are trying to verify is in a different domain, you will probably have a lock with CORS, read more at:

  

HTTP Access Control (CORS)

While it's important to know that offline sites can be anything, it may be a disabled page that issues an HTTP status code or may actually be offline ("disconnected") and will not even connect, so 13dev's answer will not work:

200: function () {
    alert('site esta on!');
},
400: function() {
    alert("site off!");
}

This script only checks the HTTP status, except that in jQuery there is no attribute method: the correct is type:

The best way to check with jQuery would be to use this:

$.ajax({
    url: "http://meusite.com",
    type: "HEAD"
}).done(function () {
     alert('Online');
}).fail(function () {
     alert('Provavelmente offline');
});

However this still has the problem of CORs probably.

The @Godfrey code may also not work in modern browsers (maybe in the future), because it's in synchronous mode and this is out of use for browsers and will soon be removed:

var xhr = new XMLHttpRequest();
xhr.open("GET",uri,false);
xhr.send(null);
if(xhr.status == 200) { // A página está online.
     return xhr.responseText;
}

It is best to use onreadystatechange :

var xhr = new XMLHttpRequest();
var uri = "http://site.com";

xhr.open("GET", uri, true); //Defina TRUE

//Usando callback
xhr.onreadystatechange = function () {
    if(xhr.status >= 200 && xhr.status < 300) { // A página está online.
         alert("online");
    } else {
         alert("provalmente offline");
    }
};
xhr.send(null);

There is no guaranteed way to check connectivity on the front end

For reasons of CORs as I said you can not check if a site is actually online, unfortunately the only reliable solution is using back-end , even if it was not what you requested yet yes I recommend you review this.

Examples in PHP:

Checking if a site is available and port 80 (which is the HTTP port is available)

if (!fsockopen("www.site-externo.com", 80, $errno, $errstr, 10)) {
    echo "Offline";
} else {
    echo "Online";
}

The previous code only checks if the site is online, but it does not check if the page you are looking for is "valid" (returns HTTP code between 200 and "299"), however if you want to check this you can also use% :

<?php

$url = 'https://www.site-externo.com/pagina-especifica';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

//Define um User-agent
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:21.0) Gecko/20100101 Firefox/21.0');

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);

//Não retorna a resposta
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);

//Resposta
$data = curl_exec($ch);

if($data === false) {
    echo 'Offline, detalhes do erro: ' . curl_error($ch);
} else {
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ($httpcode >= 200 && $httpcode < 300) {
        echo 'Online';
    } else {
        echo 'Offline, resposta HTTP: ' . $httpcode;
    }
}

Another way not so guaranteed , but still more secure than with Ajax would be to use curl with a document.createElement('script') resource of the external site.

    
24.08.2017 / 20:39
2

The simplest of all would be to make an ajax request to check if the page exists:

$.ajax({
    url: "http://meusite.com",
    type: "HEAD"
}).done(function() { 
      alert('Site existe!');
}).fail(function() { 
      alert('Site não existe!');
})

OBS Remembering that this will not work if the domain being searched is not the same as the one in the application. Because of the header Access-Control-Allow-Origin

    
24.08.2017 / 20:14
0

You have not specified whether you want to check if the page belongs to the same domain and you want the response using jQuery ... Anyway:

function checkPage() {
    // Colocar aqui a url.
    var url = ''
    var xhr = new XMLHttpRequest();
    xhr.open("GET",url ,false);
    xhr.send(null);
    if(xhr.status == 200) { // A página está online.
     return xhr.responseText;
    }

    // Está offline.
    return null;
}
    
24.08.2017 / 20:16