2010-11-18 185 views
2

我想从我的网页(HTML)打开cmd窗口。我使用JS,但有些东西不对,因为当我按下时,函数不会被调用。从浏览器运行cmd - 通过JS

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<script type="text/javascript"> 
<!-- 
function runCmd(command, option) 
{ 
    var char34 = String.fromCharCode(34); 
    var wsh = new ActiveXObject('WScript.Shell'); 
    if (wsh) 
    { 
     command = 'cmd /k ' + char34 + wsh.ExpandEnvironmentStrings(command) + ' '; 
     command = command + char34 + wsh.ExpandEnvironmentStrings(option) + char34 + char34; 
     if (confirm(command)) 
     { 
      wsh.Run(command); 
     } 
    } 
} 
//--> 
</script> 
</head> 

<body> 

<input type="button" value="Run!" onclick="runCmd(‘notepad.exe’, ‘%programfiles%\file.txt’);" /> 
</body> 
</html> 

编辑: 我救它作为PHP现在我有一个错误在FF:

ActiveXObject is not defined 
[Break on this error] var wsh = new ActiveXObject('WScript.Shell'); 

谢谢!

+0

在什么浏览器中?您正在使用IE浏览器,并且该页面位于可信网站中,对吗? (或任何需要IE浏览器运行这样的潜在危险的东西) – 2010-11-18 13:42:32

+0

在IE和FF ...是的,值得信赖的网站。 – Ronny 2010-11-18 13:43:59

+3

而你虽然'eval()'是邪恶的... – 2010-11-18 13:44:30

回答

9

您将不得不基本关闭浏览器中的所有安全功能(这需要各种Internet Explorer才能使用ActiveX)。

大多数浏览器都不允许这种类型的东西,你能想象一下,如果[随机的互联网用户]能够通过让你访问一个网页来在你的计算机上运行任何他们想要的东西?

+0

你确定这是问题吗?我试图添加'alert(“here”);'在开始处,我没有看到警报。 – Ronny 2010-11-18 13:48:16

0

根据documents

这个对象是Microsoft扩展,只在互联网 Explorer中支持...

ActiveXObject只是内部Internet Explorer,只有额外的权限和使用几个警告信息。你甚至可以考虑到,因为它暴露了客户端计算机的几个安全问题,所以其他浏览器都不支持它。