本文目录一览:
- 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