浏览器中的javascript能调用linux客户端上的上的shell程序吗

JavaScript019

浏览器中的javascript能调用linux客户端上的上的shell程序吗,第1张

首先在shell中执行js脚本需要可以执行的环境,其次调配环境,之后进行运行,具体步骤与实例如下:

①安装java、javac环境(如已安装环境请跳过此步)

yum install java-1.6.0-sun.x86_64 java-1.6.0-sun-devel.x86_64

②准备java程序RunScriptFile.java

import java.io.FileReader

import javax.script.ScriptEngine

import javax.script.ScriptEngineManager

public class RunScriptFile {

public static void main(String[] args) {

ScriptEngineManager manager = new ScriptEngineManager()

ScriptEngine engine = manager.getEngineByName("js")

try {

FileReader reader = new FileReader("testFile.js")

engine.eval(reader)

reader.close()

} catch (Exception e) {

e.printStackTrace()

}

}

}

③准备js文件testFile.js

function add(a, b) {

c = a + b

return c

}

result = add (10, 5)

print ('Result = ' + result)

④编译java程序

javac RunScriptFile.java

会在当前目录生成RunScriptFile.class文件

⑤执行程序

java RunScriptFile

⑥结果显示

Result = 15

<script language=Javascript>

function test1(){

var wsh = new ActiveXObject('WSCript.shell')

wsh.run('notepad.exe')

}

</script>

<script language=vbscript>

Function test2()

Dim wshShell

set wshShell =createObject("WSCript.shell")

wshShell.run "notepad.exe"

end Function

</script>

<input type=button value=按钮1 onclick=test1()><input type=button value=按钮2 onclick=test2()>