admin管理员组

文章数量:1304189

I know there are some browser issues associated with running a bat file from javascript, but the below code doesnt work on chrome nor on ie

<html>
<head>
<script type="text/javascript">
function runApp(which) {
  WshShell = new ActiveXObject("WScript.Shell");
  WshShell.Run (which,1,true);
}
</script>
</head>
<body>
<!-- Two ways to create a link to run the app. -->
<font onClick="runApp('file:C:/path/to/batfile.bat');" style="cursor: hand;"><u>Notepad</u>  </font>
<br>
<!-- Or use <a> descriptor -->
<a href="runApp('file://c:/test.bat');">Batch File</a>
</body>
</html>

I know there are some browser issues associated with running a bat file from javascript, but the below code doesnt work on chrome nor on ie

<html>
<head>
<script type="text/javascript">
function runApp(which) {
  WshShell = new ActiveXObject("WScript.Shell");
  WshShell.Run (which,1,true);
}
</script>
</head>
<body>
<!-- Two ways to create a link to run the app. -->
<font onClick="runApp('file:C:/path/to/batfile.bat');" style="cursor: hand;"><u>Notepad</u>  </font>
<br>
<!-- Or use <a> descriptor -->
<a href="runApp('file://c:/test.bat');">Batch File</a>
</body>
</html>
Share Improve this question asked Aug 3, 2013 at 0:06 user2205925user2205925 1171 gold badge3 silver badges7 bronze badges 5
  • 6 You can't use ActiveX stuff in non-IE browsers so trying this in Chrome is not productive. In IE, ActiveX must be enabled. stackoverflow./questions/2138002/… – Paul S. Commented Aug 3, 2013 at 0:08
  • 7 Wow a <font> tag ... wherever you got this example from, find another source. – Pointy Commented Aug 3, 2013 at 0:09
  • For local files, file url has three slashes. e.g. file:/// – mshsayem Commented Aug 3, 2013 at 0:25
  • 3 I can tell right away that this is filled with good intent. – dead beef Commented Aug 3, 2013 at 0:48
  • The above solution restricts us to run the script only on IE because of ActiveX. What about other browsers? – Praveen Pandey Commented Feb 13, 2014 at 6:21
Add a ment  | 

2 Answers 2

Reset to default 2

On IE, add this function under your function runApp(which){} in the tag

// A fucntion to run any cmd mand
function runApp(which) {
    // Using Windows Script Host Shell ActiveX to run cmd mands
    WshShell = new ActiveXObject("WScript.Shell");
    WshShell.Run("cmd /c " + which);
}
// A function to set the Optimal Internet Options
function setOptimalInternetOptionsForDevelopers() {
    // 1- Settings: Don't show the Dialog box "An ActiveX control on this page might be unsafe to interact
    // with other parts of the page. Do you want to allow this interaction?"
    // Refer to http://stackoverflow./questions/894369/an-activex-control-on-this-page-might-be-unsafe
    var registryKey = '"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\0"';
    // 1201 Value means: Initialize and script ActiveX controls not marked as safe
    // Refer to http://support.microsoft./kb/833633
    var regValName = "1201";
    // Value of 1 will show the Dialog box (Default Value)
    // Value of 0 will not show that dialog box (we need to set this value)
    var regValue = "0";
    runApp('reg add ' + registryKey + ' /v ' + regValName + ' /t REG_DWORD /d ' + regValue + ' /f');

    // 2- Internet Options => Advanced tab => Check "Allow active content to run in files on My Computer*
    // Default (1) not checked
    // we need to change it to (0) to allow this option
    // Refer to http://support.microsoft./kb/2002093
    registryKey = '"HKCU\\Software\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_LOCALMACHINE_LOCKDOWN"';
    regValName = "iexplore.exe";
    regValue = "0";
    runApp('reg add ' + registryKey + ' /v ' + regValName + ' /t REG_DWORD /d ' + regValue + ' /f');
}
Now, all you have to do is launch it in the <body> section before any script is needed to be triggered.
For Example:
<body>
    <script type="text/javascript">
        setOptimalInternetOptionsForDevelopers();
        // the rest of the content
    </script>
</body>

Note: The first time you launch this page, it will prompt a message to "Allow blocked content", just click on it to accept it. Then click "yes" on the next dialog box that appears Dialog box "An ActiveX control on this page might be unsafe to interact with other parts of the page. Do you want to allow this interaction?" This will happen only the first time you launch the page. Next time, everything will run like a charm with no more messages. Of course you can modify my function runApp(which) {} to any other name for you to work. Although the above script will work for IE, I don't think it'll work for Chrome or Firefox.

I was able to do it in IE explorer and largely your Q/A helped me a lot.

Also I would like to add that I was able to do the required with the mands from the batch pasted inside the runApp function like this

  function runApp() {
               WshShell = new ActiveXObject("WScript.Shell");
              WshShell.Run("explorer.exe \\\\hostname.remote."1,true);
               }

The extra slashes '\' are for escaping . It was quite good ! Below Q/A encouraged me also to do that VBScript: How to call Run() with parameters

本文标签: htmlHow to run a bat file using javascriptStack Overflow