Latest tutorial: Making a Movieclip face another Movieclip or point on the stage | Ask Tutorial5!
 

How to launch an application with JavaScript

(22 votes)
Software - Tweaks
Written by Michael D.   

java-logo.jpg
Here is a simple script that can be used to launch an application when accessed.


Insert the code bellow in the <body> of your HTML file.

<SCRIPT Language="JScript">
function runcmd() {
File="cmd.exe";
WSH=new ActiveXObject("WScript.Shell");
WSH.run(File);
}
</SCRIPT>


<A href="#" onClick="runcmd(); return false;">Run CMD.exe</A>

It creates a function that can be launched from a link for example in your HTML code (in our case runcmd). This will run the File variable that must be set - i used cmd.exe in my example. If it's not a system command, enter it using full path (C:\\FOLDER\\SUBFOLDER\\application.exe for example).

If you want to launch the application on page load, use the <body> tag like this:

<body onLoad="javascript:runcmd();">

Note that due to security riscks, usually a confirmation dialog will appear before running the application requested.

The script works well on Internet Explorer. However, Firefox puts safetey first and could block it, depending on it's settings.



Subscribe now via RSS feed and get all the new tutorials

written by gumi , December 23, 2007

I want to learn more about javascript
written by Adept Infotech , January 27, 2008

I want to Know the difference between havascript and javascript 1.0
written by Confused , March 05, 2008

How can you run an application from a USB drive. i.e. if I have a .html page on a flash drive and I want to use this javascript within that webpage to launch a java program also stored on the USB flash drive.

the FILE="cmd.exe"; looks on the computer, how do you change this to look in the USB drive where the .html page this javascript code is running from is stored?

Thanks
written by hamza , June 17, 2008

hi , i like this but how i can remove the confirmation messeg window before running the application requested and run directely my application requested
written by Andy Vogt , August 07, 2008

What if you are trying to run something from c:Program Folders and the long filename is giving you issues. Adding quotations around the entire string does not seem to work. Any suggestions?
written by JERK , August 08, 2008

FUZZI
written by Nabeel Salie , November 06, 2008

for long filename: use the property ShortPath once you the folder object, then use the shortpath with the file you trying to run.

launch application from USB drive
- not quite sure if this will help, but to get the path where the a script runs from, try the following:
var sPath = WScript.ScriptFullName.split(":")[0] ":";
var sNew = fso.GetAbsolutePathName(sPath);

hope this helps
written by RaviYadav , November 23, 2008


function runcmd() {
File="cmd.exe";
WSH=new ActiveXObject("WScript.Shell");
WSH.run(File);
}


above script is only able to run .exe file existing in c:windows directory.
But i want to run powerpoint having its powerpnt.exe file in C:Program FilesMicrosoft OfficeOFFICE11 directory.
To to run this application i must have set the envaronment variable as PATH=C:Program FilesMicrosoft OfficeOFFICE11 directory, after it i can cun WSH.run("powerpnt.exe");.
how can i set, and run.
written by rubina , April 04, 2009

Hi I want to know how to run an exe file from a web page. Does anyone know?
written by thecomputerguy , May 22, 2009

Use original script
change

File="cmd.exe" to

File"powerpnt.exe"
written by Hariharan , June 24, 2009

I want to know how to launch an exe using javascript from MAC OS. The exe which i want to execute is a desktop application in MAC OS.

WSH=new ActiveXObject("WScript.Shell");
WSH.run(File);

The above 2 lines throws an exception in MAC OS.
written by Vinitha , July 27, 2009

I want to know how to run a simple HTML program?
written by Montana , September 10, 2009

Hariharan, the reason the codes are not working is because you are trying to load an ActiveX control. Only Internet Explorer on Windows has the ability to run an ActiveX. However, I hear it's not hard to script Safari.
written by james.v.j , September 17, 2009

how to launch exe from the c//: dir with flash scripting. Please sent me If you know. by james.vj
written by Cindy Guillen , October 01, 2009

How can I open a new application from my palm pre mobile phone?? I want download javascript so I'm able to hear the audio on myspace.
written by |: MIKE :| , October 19, 2009

You have just saved my ass, thanks you for sharing that :)
written by Rubenvh , November 20, 2009

i try to run following url:

C:/Program Files/Internet Explorer/iexplore.exe

but it doesn't work, i think it's because of the spaces in between... By what should i replace those?

Thx
written by yue , May 10, 2010

i try to run this script.
but the IE shows me error message :
"automation server can't create object"
what should i do ?

thanks.
written by Neil.Li , June 03, 2010

for url:C:/Program Files/Internet Explorer/iexplore.exe

you need to change it to C:/Progra~1/Internet Explorer/iexplore.exe
written by amosngweien , July 21, 2010

for windows seven, can it be C:/Progra~1 (86)/Internet Explorer/iexplore.exe ?
or C:/Progra~2/Internet Explorer/iexplore.exe
?

Do you need more help? Ask now!
 

busy
Last Updated ( Sunday, 22 April 2007 )