用JS执行本地可执行文件

      客户端脚本(比如Javascipt)本身没有权限进行系统调用,但是在windows 系统下,在IE浏览器中利用JS创建activeX对象可以调用本地的可执行文件:

<script language="javascript" type="text/javascript">
	function runExe() 
	{ 
        //创建ActiveX对象
		var shell = new ActiveXObject("WScript.shell"); 
		//命令字符串,路径不能为反斜杠且路径中的文件夹名称中不得有空格,
        //如果出现空格,会被解析成两个命令
		var cmd="d:/test/target.exe";
		//true的t小写
		shell.run(cmd,1,true); 
	} 
</script>

       其中的shell.run参数介绍如下:

       参数1:执行命令字符串;

       参数2:应用执行时的窗口风格,1表示激活并显示窗口;

       参数3:是否等待命令执行完再向下执行,true表示等待。

 

 

 

参考资料:

How to run an executable program on the client?

Client-side scripts themselves cannot make system calls but on Windows systems you can insert Windows Script Host commands in client-side script.

To launch an application on the client machine, place this script in the head of the HTML page.

<script language="javascript" type="text/javascript">
function runApp() 
{ 
var shell = new ActiveXObject("WScript.shell"); 
shell.run("notepad.exe", 1, true); 
} 
</script>


Place this in the body of the HTML page. The button click will trigger off the function and the application is run.

<input type="button" name="button1" value="Run Notepad" onClick="runApp()" />


var shell = new ActiveXObject("WScript.shell");


Creates the WSH object as a new instance of an ActiveXObject.

shell.run("notepad.exe", 1, true);


The run method of WSH starts the executable program.

The first parameter passed into the run method executes the command "notepad.exe".

The second parameter is optional and its integer value specifies the window style of the application launched. The 1 value will activate and display the window.

The third parameter is optional and its boolean value specifies if it is to wait for the command to complete before further execution. The true value will wait for the completion of the command.

Note that running such scripts properly require to change the client's security settings.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值