
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to Ping a Server using JavaScript?
A server ping can be defined as hitting a server and getting a response in return from that server. The idea is to send an echo message that will keep the health check and check whether the server is up and running or not. On sending a PING every server sends a PONG that shows that the server is active. Ping messages are sent by the ICMP (Internet Control Messaging Protocol). The lower the ping time the stronger the connection between the host and the server.
Approach
We can use a JavaScript function for sending the Ping messages to the server. In this tutorial, we will be sending the Pings by using the AJAX functionality and then displaying the response once the Pong is received. We will also examine the status code received to find whether the server is active or not. If any status other than 200 is received it means the server is not active or not working properly.
Example
In the below example, we have created a simple HTML page that will ping a specific path and return its response.
# index.html
<!DOCTYPE html> <html> <head> <title> Pinging the Server </title> <script type="text/javascript" src="https://code.jquery.com/jquery-1.7.1.min.js"></script> <style type="text/css"> .responded { color:green; } .checking,.unchecked { color:#FF8C00; } .timeout { color:red; } </style> </head> <body> <h2 style="color:green"> Welcome To Tutorials Point </h2> <label for="url"> Enter the URL you want to ping: </label><br> <input type="text" id="url"name="url" style="margin: 10px; width: 50%;"><br> <input type="submit" value="Submit"onclick="pingURL()"> <div id="outputDiv"></div> </body> <script> 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); }); } </script> </html>
Output
- Related Articles
- How to ping external IP from java Android?
- How to interface Arduino with a GSM Module and ping to a website?
- Linux ping Command
- How to Ping External host from Swift in iOS?
- How to build JCo server without using a Properties file in SAP?
- How to copy a file to a remote server in Python using SCP or SSH?
- How to enable credssp authentication on windows server using PowerShell?
- How to disable basic authentication for windows server using PowerShell?
- How to copy files from one server to another using Python?
- How to display the domain of the server that loaded a document in JavaScript?
- How to Configure Nagios Server for Monitoring Apache Server
- Connect to external server by using phpMyAdmin
- Fping – A Command-Line Tool to Ping Hosts In Parallel on Ubuntu
- How to create a mock server in Postman?
- How to restart MySQL server?
