当前位置: 代码迷 >> JavaScript >> 用JS施行本地可执行文件
  详细解决方案

用JS施行本地可执行文件

热度:173   发布时间:2012-11-17 11:14:14.0
用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.