- 1、node.js怎么调用ping
- 2、能否使用JavaScript来ping一个IP?
- 3、求一段JS代码,如果本域名主机下“/web/111.html”网页存在,则alert(“YES”),否则alert(“NO”)
- 4、用Javascript调用cmd并执行命令
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 是为了解码中文
可以在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的返回值就可以判断了。
使用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
});
建立test.bat文件,存于D:根目录下,作用是将*txt文件拷贝到d:/test目录下。
md test
copy d:/*.txt d:/test
pause
创建WScript.Shell对象,由该对象直接运行test.dat文件。
var objShell
objShell=new ActiveXObject(“WScript.Shell”)
var iReturnCode=objShell.Run(“c:/test.bat”,0,true)
创建WScript.Shell对象,由该对象直接运行CMD命令。
var objShell
var objShell= new ActiveXObject(“WScript.Shell”)
var iReturnCode=objShell.Run(“cmd.exe /c md test”,0,true)
iReturnCode=objShell.Run(“cmd.exe /c copy d:/*.text mytest”,0,true)
在js中调用php的代码:
SCRIPT Language = “JavaScript”
function func()
{ if(confirm(“Are you OK with this?”))
{ this.location = “ok.php?action=ok”; }
else
{ this.location = “ok.php?action=cancel”; } }
/SCRIPT
html
head
/head
body
a href=”#” href=”#” onClick=”javascript:func();”Please Click/a
/body
/html
if($_GET[“action”]==”ok”)
{ echo “I’m OK!”; }
else
{echo “I’m not OK!”; }
原创文章,作者:BF2Z5,如若转载,请注明出处:https://www.506064.com/n/126487.html