本文目錄一覽:
- 1、使用jquery插件測試url鏈接是否有效及js ping url
- 2、node.js怎麼調用ping
- 3、求一段JS代碼,如果本域名主機下“/web/111.html”網頁存在,則alert(“YES”),否則alert(“NO”)
- 4、能否使用JavaScript來ping一個IP?
- 5、js檢測網站是否能打開
使用jquery插件測試url鏈接是否有效及js ping url
要用的需要明白代碼及自己改些地方
(function($) {
$.fn.ping = function(options) {
var opts = $.extend({}, $.fn.ping.defaults, options);
var stime = new Date().getTime();
return this.each(function() {
var ping, requestTime, responseTime ;
var target = $(this);
function ping(){
node.js怎麼調用ping
let ping = require(‘child_process’).spawn(‘ping’,[‘127.0.0.1’]);
let iconv = require(‘iconv-lite’);
ping.stdout.on(‘data’,data={
let str = iconv.decode(data,’cp936′);
console.log(str);
})
ping.stderr.on(‘data’,data={
console.log(data);
})
ping.on(‘close’,code={
console.log(‘Ping 結束’)
})
//之前是通過exec 來調用,不過當時沒有進行測試,想當然的認為是可以的,抱歉。
//以上引入iconv-lite 是為了解碼中文
求一段JS代碼,如果本域名主機下“/web/111.html”網頁存在,則alert(“YES”),否則alert(“NO”)
使用jQuery模擬PING:
$.ping = function(option) { var ping, requestTime, responseTime ; var getUrl = function(url){ //保證url帶http:// var strReg=”^((https|http)?://){1}” var re=new RegExp(strReg); return re.test(url)?url:”http://”+url; } $.ajax({ url: getUrl(option.url)+’/’+ (new Date()).getTime() + ‘.html’, //設置一個空的ajax請求 type: ‘GET’, dataType: ‘html’, timeout: 10000, beforeSend : function() { if(option.beforePing) option.beforePing(); requestTime = new Date().getTime(); }, complete : function() { responseTime = new Date().getTime(); ping = Math.abs(requestTime – responseTime); if(option.afterPing) option.afterPing(ping); } }); if(option.interval option.interval 0) { var interval = option.interval * 1000; setTimeout(function(){$.ping(option)}, interval);// option.interval = 0; // 阻止多重循環// setInterval(function(){$.ping(option)}, interval); }};
//栗子 HTML
div id=”msg”/div
$.ping({
url : ”,
beforePing : function(){$(‘#msg’).html(”)},
afterPing : function(ping){$(‘#msg’).html(ping)},
interval : 1
});
能否使用JavaScript來ping一個IP?
可以在javascript中ping一個ip地址:
以下是自定義的實現函數:
function Pinger_ping(ip, callback) {
if(!this.inUse) {
this.inUse = true;
this.callback = callback
this.ip = ip;
var _that = this;
this.img = new Image();
this.img.onload = function() {_that.good();};
this.img.onerror = function() {_that.good();};
this.start = new Date().getTime();
this.img.src = “http://” + ip;
this.timer = setTimeout(function() { _that.bad();}, 1500);
}
}
用法如下:
傳入ip和callback參數:比如192.0.8.10 和 mycallBack
setTimeout的返回值就可以判斷了。
js檢測網站是否能打開
找到一個ping的代碼
!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “”
html xmlns=””
head
meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″ /
titleHTTP Ping/title
style
html {
height: 100%;
overflow: hidden;
}
body {
background: #000;
color: #C0C0C0;
font-weight: bold;
font-size: 14px;
font-family: Lucida Console;
height: 100%;
margin: 0 0 0 5px;
}
#divInput {
height: 10%;
}
#divContent {
height: 90%;
overflow: auto;
}
#txtTimeout {
width: 40px;
}
button {
margin-left: 10px;
}
/style
/head
body
div id=”divInput” spanURL:/span
input id=”txtURL” type=”text” /
spanTimeout:/span
input id=”txtTimeout” type=”text” value=”2000″ /
input id=”btnSwitch” type=”button” value=”Start” onclick=”handleBtnClick()” /
hr/
/div
div id=”divContent”/div
script
var intStartTime;
var objIMG = new Image();
objIMG.onload =
objIMG.onerror =
function()
{
/*
* 有回應,取消超時計時
*/
clearTimeout(intTimerID);
if(!bolIsRunning || bolIsTimeout)
return;
var delay = new Date() – intStartTime;
println(“Reply from ” +
strURL +
” time” +
((delay1)?(“1”):(“=”+delay)) +
“ms”);
arrDelays.push(delay);
/*
* 每次請求間隔限制在1秒以上
*/
setTimeout(ping, delay1000?(1000-delay):1000);
}
function ping()
{
/*
* 發送請求
*/
intStartTime = +new Date();
intSent++;
objIMG.src = strURL + “/” + intStartTime;
bolIsTimeout = false;
/*
* 超時計時
*/
intTimerID = setTimeout(timeout, intTimeout);
}
function timeout()
{
if(!bolIsRunning)
return;
bolIsTimeout = true;
objIMG.src = “X:\\”;
println(“Request timed out.”);
ping();
}
/script
script
var $ = function(v){return document.getElementById(v)};
var arrDelays = [];
var intSent;
var bolIsRunning = false;
var bolIsTimeout;
var strURL;
var intTimeout;
var intTimerID;
var objBtn = $(“btnSwitch”);
var objContent = $(“divContent”);
var objTxtURL = $(“txtURL”);
objTxtURL.value = window.location.host;
function handleBtnClick()
{
if(bolIsRunning)
{
/*
* 停止
*/
var intRecv = arrDelays.length;
var intLost = intSent-intRecv;
var sum = 0;
for(var i=0; iintRecv; i++)
sum += arrDelays[i];
objBtn.value = “Start”;
bolIsRunning = false;
/*
* 統計結果
*/
println(“ ”);
println(“Ping statistics for ” + strURL + “:”);
println(“Packets: Sent = ” +
intSent +
“, Received = ” +
intRecv +
“, Lost = ” +
intLost +
” (” +
Math.floor(intLost / intSent * 100) +
“% loss),”);
if(intRecv == 0)
return;
println(“Approximate round trip times in milli-seconds:”);
println(“Minimum = ” +
Math.min.apply(this, arrDelays) +
“ms, Maximum = ” +
Math.max.apply(this, arrDelays) +
“ms, Average = ” +
Math.floor(sum/intRecv) +
“ms”);
}
else
{
/*
* 開始
*/
strURL = objTxtURL.value;
if(strURL.length == 0)
return;
if(strURL.substring(0,7).toLowerCase() != “http://”)
strURL = “http://” + strURL;
intTimeout = parseInt($(“txtTimeout”).value, 10);
if(isNaN(intTimeout))
intTimeout = 2000;
if(intTimeout 1000)
intTimeout = 1000;
objBtn.value = “Stop “;
bolIsRunning = true;
arrDelays = [];
intSent = 0;
cls();
println(“Pinging ” + strURL + “:”);
println(“ ”);
ping();
}
}
function println(str)
{
var objDIV = document.createElement(“div”);
if(objDIV.innerText != null)
objDIV.innerText = str;
else
objDIV.textContent = str;
objContent.appendChild(objDIV);
objContent.scrollTop = objContent.scrollHeight;
}
function cls()
{
objContent.innerHTML = “”;
}
/script
/body
/html
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/271815.html