38

Is it possible to run bat/executable file using html5 button event? In IE its achievable using Shell object if I am not wrong.

tckmn
  • 57,719
  • 27
  • 114
  • 156
VSe
  • 919
  • 2
  • 13
  • 29
  • 2
    See this link [How to execute a Local File using HTML Application?](http://www.codeproject.com/Articles/113678/How-to-execute-a-Local-File-using-HTML-Application) – coolprarun Sep 24 '13 at 12:00
  • @coolprarun I also confirmed that the link you provide works in my environment (`Win8.1`, `IE11`. Not on `FF27`). You should post it as an answer. – IsaacS Mar 26 '14 at 16:35

5 Answers5

39

No, that would be a huge security breach. Imagine if someone could run

format c:

whenever you visted their website.

tckmn
  • 57,719
  • 27
  • 114
  • 156
24

Here's what I did. I wanted a HTML page setup on our network so I wouldn't have to navigate to various folders to install or upgrade our apps. So what I did was setup a .bat file on our "shared" drive that everyone has access to, in that .bat file I had this code:

start /d "\\server\Software\" setup.exe

The HTML code was:

<input type="button" value="Launch Installer" onclick="window.open('file:///S:Test/Test.bat')" />

(make sure your slashes are correct, I had them the other way and it didn't work)

I preferred to launch the EXE directly but that wasn't possible, but the .bat file allowed me around that. Wish it worked in FF or Chrome, but only IE.

user247702
  • 23,641
  • 15
  • 110
  • 157
Chuff
  • 264
  • 2
  • 2
17

It is possible when the page itself is opened via a file:/// path.

<button onclick="window.open('file:///C:/Windows/notepad.exe')">
    Launch notepad
</button>

However, the moment you put it on a webserver (even if you access it via http://localhost/), you will get an error:

Error: Access to 'file:///C:/Windows/notepad.exe' from script denied

user247702
  • 23,641
  • 15
  • 110
  • 157
  • 1
    +1 I just confirmed that this works on IE11, but does not on Firefox 27 on Win8.1. – IsaacS Mar 26 '14 at 15:37
  • 1
    @IsaacS I don't have access to a Windows 8 box so unfortunately I can't help with that. I've just tried this again with Firefox 28 on Windows 7 and it works, so the additional security is probably limited to Windows 8. – user247702 Mar 26 '14 at 15:41
  • 4
    This allows to "download" notepad.exe, not run it. Firefox 31, Windows 7 – mp04 Aug 04 '14 at 00:55
  • @nodiscc Indeed, Firefox by default does not allow you to run executables, but it's not uncommon to have an extension like [OpenDownload²](https://addons.mozilla.org/en-US/firefox/addon/opendownload-10902/) to allow you to do so. – user247702 Aug 04 '14 at 07:13
  • 2
    @Stijn the file that gets executed in this case is the file you just "downloaded", not the original. So in most cases it does not work - it doesn't allow you to run programs already installed on your machine. – mp04 Aug 04 '14 at 15:08
  • @nodiscc correct, it doesn't execute from the original application but from the cache. If it works or not depends on the executable needing access to other files in its directory. Thank you for your comments. – user247702 Aug 04 '14 at 15:17
6

You can do it on Internet explorer with OCX component and on chrome browser using a chrome extension chrome document in any case need additional settings on the client system!

Important part of chrome extension source:

var port = chrome.runtime.connectNative("your.app.id"); 
      port.onMessage.addListener(onNativeMessage); 
      port.onDisconnect.addListener(onDisconnected);
      port.postMessage("send some data to STDIO");

permission file:

{
      "name": "your.app.id",
      "description": "Name of your extension",
      "path": "myapp.exe",
      "type": "stdio",
      "allowed_origins": [
            "chrome-extension://IDOFYOUREXTENSION_lokldaeplkmh/"
      ]
}

and windows registry settings:

HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts\your.app.id
REG_EXPAND_SZ : c:\permissionsettings.json
Stefan Kanev
  • 311
  • 4
  • 7
1

You can not run/execute an .exe file that is in the users local machine or through a site. The user must first download the exe file and then run the executable file.
So there is no possible way

The following code works only when the EXE is Present in the User's Machine.

<a href = "C:\folder_name\program.exe">

coolprarun
  • 1,153
  • 2
  • 15
  • 22
  • 2
    Dear @coolprarun if I have a bat file in user's machine, How can I run the bat file in chrome using javascript or sth else? I could do it in IE browser through ActiveXObject, However this object is not applicable for chrome. – Hosein Aqajani Dec 09 '15 at 07:58
  • Sure you can. With custom URL protocol -> https://stackoverflow.com/questions/3057576/how-to-launch-an-application-from-a-browser – The incredible Jan Sep 10 '21 at 11:22