Is possible do a ping to a server and know if the server is online or offline without use AJAX?
I found this code but use AJAX
function pingURL() {
// Getting the URL from the User
var URL = $("#url").val();
var settings = {
// Defining the request configuration
cache: false,
dataType: "jsonp",
crossDomain: true,
url: URL,
method: "GET",
timeout: 5000,
headers: {accept: "application/json", "Access-Control-Allow-Origin": "*",},
// Defines the response to be made
// for certain status codes
statusCode: {
200: function (response) {
document.getElementById("outputDiv").innerHTML="<h3 style='color:green'>Status 200: Page is up!";
400: function (response) {
document.getElementById("outputDiv").innerHTML="<h3 style='color:red'>Status 400: Page is down.</h3>";
0: function (response) {
document.getElementById("outputDiv").innerHTML="<h3 style='color:red'>Status 0: Page is down.</h3>";
// Sends the request and observes the response
$.ajax(settings).done(function (response) {
console.log(response);
.fail(function (response) {
console.log("Error" + response);
I also other code:
function ping(extServer){
var ImageObject = new Image();
ImageObject.src = "http://"+extServer+";
if(ImageObject.height>0){
alert("Servidor en Linea !");
} else {
alert("Servidor fuera de linea :(");
Hovever with this last code , the system alway return OffLine
What other option can exist?
Well the imageobject will have no height if what you make the SRC isnt an image.
If you go back and look at the code you copied, you’ll see they’re pulling an image file from the remote server.
Let’s clarify a few things;
You’re not pinging. That’s a specific request type (ICMP), that Javascript cannot mimic. What you’re doing is making an HTTP request to the server.
Your request will be JAX. It may be AJAX, it may be SJAX (Yes, i’m inventing that term).
Why are you so opposed to the AJAX method? It’s the “right” way to do it…
The system not allow the use of this instructions
// Sends the request and observes the response
$.ajax(settings).done(function (response) {
console.log(response);
Only allow JS
It needs to be pure JavaScript
In the configuration I only can put a link to a file JavaScript.
I am testing my code in
(function () {
var URL = "https://playcode.io/javascript";
var settings = {
// Defining the request configuration
cache: false,
dataType: "jsonp",
crossDomain: true,
url: URL,
method: "GET",
timeout: 5000,
headers: {accept: "application/json", "Access-Control-Allow-Origin": "*",},
// Defines the response to be made
// for certain status codes
statusCode: {
200: function (response) {
document.getElementById("outputDiv").innerHTML="<h3 style='color:green'>Status 200: Page is up!";
400: function (response) {
document.getElementById("outputDiv").innerHTML="<h3 style='color:red'>Status 400: Page is down.</h3>";
0: function (response) {
document.getElementById("outputDiv").innerHTML="<h3 style='color:red'>Status 0: Page is down.</h3>";
$.ajax(settings).done(function (response) {
console.log(response);
.fail(function (response) {
console.log("Error" + response);
})();
And this code generate this error JavaScript Playground (playcode.io)
ReferenceError: $ is not defined
at :30:3
at :35:3
at mn (:16:5455)
Yes, because $ is a jQuery reference. So it wont be defined if you dont have jQuery loaded.
If you’re trying to do the ajax request purely in vanilla JS;
const myRequest = new Request(aURLgoeshere);
fetch(myRequest).then((response) => {
if (response.status == 200) { //Do whatever you were gonna do for a 200. }
else { //Do something else. }
If you’re purely trying to figure out if a server exists, you can no-cors the request;
fetch("http://google.com",{mode: "no-cors"}).then((response) => {
console.log("Online");
}).catch(e => { console.log("Offline")});
Note that you must get the protocol correct; The above will tell you Offline. But changing the url to https://google.com will tell you it’s Online.
This will NOT tell you if a page exists or not; it will simply tell you it found a server on the other end of its request.
Hi m_hutley
We use a card that when the enduser press this card the system call the code JS
However this only work when the user upload the page the first time, if press the card new this follow with the same value of return
How can avoid this?
Regards