- 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/zh-hant/n/126487.html