How to ping a server using JavaScript ? Last Updated : 02 Aug, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Pinging a server is used to determine whether it is online or not. The idea is to send an echo message to the server (called ping) and the server is expected to reply back with a similar message (called pong). Ping messages are sent and received by using ICMP (Internet Control Messaging Protocol). The lower the ping time, the stronger is the connection between the host and the server. Approach: One obvious approach is to use the command prompt for sending ping messages to the server. Please refer to this post for details. In this article, we'll be using Ajax to send a request to the required server and then examining the status code received to find whether the server is running or not. The idea is that a server is definitely up and running if it returns a status code 200. Other status codes like 400 etc. point toward a possible server outage. Below is the step by step implementation:Step 1: Create a file named 'index.html' file to design the basic web page. The function "pingURL" is invoked when a user clicks on the button on the web page. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <script src="index.js"></script> <script type="text/javascript" src= "https://code.jquery.com/jquery-1.7.1.min.js"> </script> <title>Ping a server using JavaScript</title> </head> <body> <label for="url"> Enter the URL you want to ping: </label><br> <input type="text" id="url" name="url" style="margin: 10px;"><br> <input type="submit" value="Submit" onclick="pingURL()"> </body> </html> Step 2: Create the 'index.js' file to make a request to the server. The "pingURL" function defined the required configuration for the Ajax request in the 'settings' variable. index.js function pingURL() { // The custom URL entered by user var URL = $("#url").val(); var settings = { // Defines the configurations // for the request cache: false, dataType: "jsonp", async: true, crossDomain: true, url: URL, method: "GET", headers: { accept: "application/json", "Access-Control-Allow-Origin": "*", }, // Defines the response to be made // for certain status codes statusCode: { 200: function (response) { console.log("Status 200: Page is up!"); }, 400: function (response) { console.log("Status 400: Page is down."); }, 0: function (response) { console.log("Status 0: Page is down."); }, }, }; // Sends the request and observes the response $.ajax(settings).done(function (response) { console.log(response); }); } OutputOpen the web page in your web browser.Press 'Ctrl+Shift+I' to navigate to Browser Developer Tools.Enter the URL you wish to ping in the form input and click the 'Submit' button. Comment More infoAdvertise with us Next Article JavaScript Post Request Like a Form Submit N neeraj26pathak Follow Improve Article Tags : JavaScript Web Technologies javascript-object JavaScript-Questions Similar Reads How to set location and location.href using JavaScript ? In this article, we will set the location and location.href using Javascript.Both location and location.href are used to set or return the complete URL of your current page. They return a string that contains the entire URL with the protocol.Syntax:location = "https://www.geeksforgeeks.org";orlocati 1 min read How to Extract the Host Name from URL using JavaScript? Extracting the hostname from a URL using JavaScript means retrieving the domain part from a complete web address. This can be done using JavaScript's URL object or methods like window.location, which allow easy access to the hostname of a URL.What is URL?A URL (Uniform Resource Locator) is the web a 2 min read How to get client IP address using JavaScript? Knowing a client's IP address can be useful for various purposes like personalizing content, tracking user activity, or offering location-based services. However, JavaScript running in the browser doesnât have direct access to this information due to security reasons. Using External APIs to Get the 3 min read JavaScript Post Request Like a Form Submit Many web developers face the challenge of emulating the functionality of a form submission using a JavaScript POST request in situations where a form is not available or appropriate. The problem requires finding a way to replicate the behavior of a form submission using JavaScript, with the goal of 4 min read How to send push notification using XMPP Server? In this article, we are going to learn how can we send push notifications using the XMPP server. XMPP stands for extensible Messaging and Presence Protocol and it is a free open-source, cross-platform web server solution stack package. which is easy to install and contains mainly the Apache HTTP Ser 2 min read How to Make a Beep Sound in JavaScript? To make a beep sound in JavaScript, you can use the Audio object to play a sound file.Approach: Using Audio FunctionUse the Audio function in Javascript to load the audio file. This HTML document creates a simple web page with a heading and a button. When the button is clicked, the play() function i 1 min read Like