How to ping a server using JavaScript ? Last Updated : 02 Aug, 2024 Comments Improve Suggest changes 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 How to ping a server using JavaScript ? 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? Imagine your computer as your apartment. Just like each apartment has a unique address for receiving mail, your computer has an IP address that helps it receive information from the internet. This IP address acts like a label that identifies your specific device on the vast network of computers, ens 2 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 How to get protocol, domain and port from URL using JavaScript ? The protocol, domain, and port of the current page can be found by two methods: Method 1: Using location.protocol, location.hostname, location.port methods: The location interface has various methods that can be used to return the required properties. The location.protocol property is used to return 2 min read How to Create HTTPS Server with Node.js ? Creating an HTTPS server in Node.js ensures secure communication between your server and clients. HTTPS encrypts data sent over the network, providing a layer of security essential for handling sensitive information. This guide will walk you through the process of setting up an HTTPS server in Node. 4 min read JavaScript - How to Return Current URL for Share Button? In this article, we will learn how to create share buttons on websites, allowing users to share a post or the URL of the site on various social media platforms. We will break this down into simple steps for better understanding.Step 1: Create the HTML StructureFirst, we need to create an HTML file t 4 min read How to call web services in HTML5 ? In this article, we will see how to call the Web Services in HTML5, along with knowing different methods for calling the web services & understand them through the examples. Web services in HTML5 are a set of open protocols and standards that allow data to be exchanged between different applicat 3 min read Like