faqts : Computers : Programming : Languages : JavaScript : Windows

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

231 of 260 people (89%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

JavaScript: File: Executable: Run: How to: How to run executable file in JavaScript? [parameter]

Nov 2nd, 2007 21:26
siobara naska, Knud van Eeden, http://siobaranaska.spaces.live.com/


----------------------------------------------------------------------
--- Knud van Eeden --- 27 September 2004 - 06:24 pm ------------------
JavaScript: File: Executable: Run: How to: How to run executable file 
in JavaScript? [parameter]
---
You might want to start and run executable programs for automating
tasks purposes.
---
E.g. You can run a sequence of commands automatically (similar to a 
batch file)
     by clicking on a button in your web browser.
---
---
1. Method: Using document.location.href
2. Method: Using the Microsoft Windows WSH script
3. Method: Using <A HREF=""> in HTML
4. Method: Save your file with the '.hta' extension
---
---
1. Method: Using document.location.href
---
In general,
you can load and or run ANY executable file
(thus files with extension .exe, .com or .bat, ...),
on the client side (or server side) in your browser,
via the JavaScript command:
 document.location.href = <your filename>
---
 document.location.href="yourexefile";
 or
 document.location.href="yourcomfile";
 or
 document.location.href="yourbatchfile";
(the only price you have to pay here, is always an extra confirmation
 click for that file, to 'Open' or 'Run this program from its current
 location', when asked for)
---
---
 e.g.: Run MySql from JavaScript
--- cut here: begin --------------------------------------------------
  document.location.href="c:/program files/merlin/bin/mysql.exe";
--- cut here: end ----------------------------------------------------
---
---
 e.g. Run Excel from JavaScript
--- cut here: begin --------------------------------------------------
<SCRIPT>
function PROCTest() {
 document.location.href = "c:/program files/microsoft 
office/office11/excel.exe";
}
</SCRIPT>
--- cut here: end ----------------------------------------------------
 e.g. Run Excel (with a filename as parameter) from JavaScript
e.g.
 c:/program files/microsoft 
office/office11/excel.exe "c:/myshare\Numbers for test calls.xls"
  becomes after replacing the space and percent character:
--- cut here: begin --------------------------------------------------
<SCRIPT>
function PROCTest() {
 document.location.href = "c:/program%20files/microsoft%
20office/office11/excel.exe%20c:/myshare/Numbers%20for%20test%
20calls.xls";
}
</SCRIPT>
--- cut here: end ----------------------------------------------------
---
---
Note:
Executable filename with parameters
---
In the case you want to call your executable filename with parameters
 e.g.
  c:/program files/microsoft office/office11/excel.exe "c:/temp/my 
excelfilename.xls"
it might be better to put this in a batch file.
As trying to pass this parameter directly seems not directly possible
(e.g. trying to type
  c:/program files/microsoft office/office11/excel.exe "c:/temp/my 
excelfilename.xls"
directly in the URL field of your browser also does not work.
So instead call a batch file containing your executable name and the 
parameters.
E.g.
Save this in mybatchfile.bat
--- cut here: begin --------------------------------------------------
c:/program files/microsoft office/office11/excel.exe "c:/temp/my 
excelfilename.xls"
--- cut here: end ----------------------------------------------------
and call this batch file instead:
--- cut here: begin --------------------------------------------------
<SCRIPT>
function PROCTest() {
 document.location.href = "c:/temp/mybatchfile.bat"
}
</SCRIPT>
--- cut here: end ----------------------------------------------------
---
---
Note:
Single backslashes in the filename:
In the case your filename contains single backslashes:
 e.g.
  c:\temp\myfile.exe
as JavaScript removes the single backslash,
remember to replace this backslashes in your filename:
1. use instead a double backslashes:
  e.g.
   c:\\temp\\myfile.exe
or
2. use instead one forward slash:
 e.g.
  c:/temp/myfile.exe
---
---
2. Method: Using the Microsoft Windows WSH script
Here an example of how to call Excel (with the filename between double 
quotes),
with your filename as a parameter (with the filename between double 
quotes).
So you enclose the whole between single quotes, and call the shell.run
method.
--- cut here: begin --------------------------------------------------
<!------------------------------------------------------------------->
<HTML>
<!------------------------------------------------------------------->
<SCRIPT
  LANGUAGE="javascript"
  TYPE="text/javascript"
>
function PROCTest() {
 var myshell = new ActiveXObject( "WScript.shell" );
 myshell.run( '"c:/program files/microsoft 
office/office11/excel.exe" "c:/yourfilename.xls"', 1, true );
}
</SCRIPT>
<!------------------------------------------------------------------->
<INPUT
  TYPE="button"
  NAME="button1"
  VALUE="Run Excel and start it with your filename"
  ONCLICK="PROCTest()"
>
</INPUT>
<!------------------------------------------------------------------->
</HTML>
<!------------------------------------------------------------------->
--- cut here: end ----------------------------------------------------
---
---
Note:
It seems not possible to run this executable by executing a .html file
located on another computer on the local network.
You will get 'error on page' if you try this.
But the same file stored on your own computer executes without
problems.
---
---
3. Method: Using <A HREF=""> in HTML
(if you use pure HTML, use
  <A HREF="yourexefile"> run your exe file </a>
  or
  <A HREF="yourcomfile"> run your com file </a>
  or
  <A HREF="yourbatchfile"> run your batch file </a>
)
---
for example:
  <A
    HREF="c:\program files\merlin\bin\mysql.exe"
  >
   Run your exe file
  </A>
---
---
4. Method: Save your file with the '.hta' extension instead of with 
the '.htm' extension
   (a .hta file is a 'trusted' htm file in Microsoft Explorer)
This should be one of the best methods.
--- cut here: begin --------------------------------------------------
<!-------------------------------------------------------------------->
<HTML>
<!-------------------------------------------------------------------->
<BODY
  ONLOAD='
   var Shell = new ActiveXObject("WScript.Shell");
   Shell.Run("file:///c|/windows/notepad.exe");
  '
  >
<!-------------------------------------------------------------------->
</BODY>
<!-------------------------------------------------------------------->
</HTML>
<!-------------------------------------------------------------------->
--- cut here: end ----------------------------------------------------
e.g. save this file as 'myexecutable.hta', and run it e.g. from
the command line by typing 'start myexecutable.hta'
--- cut here: begin --------------------------------------------------
<!-------------------------------------------------------------------->
<HTML>
<!-------------------------------------------------------------------->
<SCRIPT>
function PROCRun() {
 var shell = new ActiveXObject("WScript.Shell");
 shell.run( '"C:/Program Files/your executable.exe"', 1, true );
 window.close();
}
</SCRIPT>
<!-------------------------------------------------------------------->
<BODY
  ONLOAD='
   PROCRun();
  '
>
<!-------------------------------------------------------------------->
</BODY>
<!-------------------------------------------------------------------->
</HTML>
<!-------------------------------------------------------------------->
--- cut here: end ----------------------------------------------------
===
e.g. run Lotus Notes by calling the function with the program name as a
parameter
--- cut here: begin --------------------------------------------------
<!-------------------------------------------------------------------->
<HTML>
<!-------------------------------------------------------------------->
<SCRIPT>
function PROCJavascriptRunProgramParameter( programNameS, 
parameterS ) 
{
 var shell = new ActiveXObject("WScript.Shell");
 var quoteS = String.fromCharCode( 34 );
 shell.run( quoteS + programNameS + quoteS, 1, false );
 self.close();
}
</SCRIPT>
<!-------------------------------------------------------------------->
<BODY
  ONLOAD='
   PROCJavascriptRunProgramParameter( "C:/Program 
Files/lotus/notes/notes.exe", "" );
  '
>
<!-------------------------------------------------------------------->
</BODY>
<!-------------------------------------------------------------------->
</HTML>
<!-------------------------------------------------------------------->
--- cut here: end ----------------------------------------------------
===
e.g. run Lotus Notes by calling the function with the program name as a
parameter, and starting it with your local database as a parameter
--- cut here: begin --------------------------------------------------
<!-------------------------------------------------------------------->
<HTML>
<!-------------------------------------------------------------------->
<SCRIPT>
function PROCJavascriptRunProgramParameter( programNameS, 
parameterS ) 
{
 var shell = new ActiveXObject("WScript.Shell");
 var quoteS = String.fromCharCode( 34 );
 shell.run( quoteS + programNameS + quoteS + " " + parameterS, 1, 
false );
 self.close();
}
</SCRIPT>
<!-------------------------------------------------------------------->
<BODY
  ONLOAD='
   PROCJavascriptRunProgramParameter( "C:/Program 
Files/lotus/notes/notes.exe", "names.nsf" );
  '
>
<!-------------------------------------------------------------------->
</BODY>
<!-------------------------------------------------------------------->
</HTML>
<!-------------------------------------------------------------------->
--- cut here: end ----------------------------------------------------
===
e.g. run Lotus Notes by calling the function with the program name as a
parameter, and starting it with your database as a parameter
(you have to give the server and the database name as a parameter)
--- cut here: begin --------------------------------------------------
<!-------------------------------------------------------------------->
<HTML>
<!-------------------------------------------------------------------->
<SCRIPT>
function PROCJavascriptRunProgramParameter( programNameS, 
parameterS ) 
{
 var shell = new ActiveXObject("WScript.Shell");
 var quoteS = String.fromCharCode( 34 );
 shell.run( quoteS + programNameS + quoteS + " " + parameterS, 1, 
false );
 self.close();
}
</SCRIPT>
<!-------------------------------------------------------------------->
<BODY
  ONLOAD='
   PROCJavascriptRunProgramParameter( "C:/Program 
Files/lotus/notes/notes.exe", "yourservername!!yourdatabase.nsf" );
  '
>
<!-------------------------------------------------------------------->
</BODY>
<!-------------------------------------------------------------------->
</HTML>
<!-------------------------------------------------------------------->
--- cut here: end ----------------------------------------------------
---
---
Internet: see also:
---
[Internet: see also: http://www.google.com search for 'executable 
JavaScript parameter': 
http://www.codepedia.com/1/How_To_Javascript_Run_Executable_On_Clie
nt]
---
What software must a client download and install so I can run a local 
disk (*.exe) from http page?
http://www.faqts.com/knowledge_base/view.phtml/aid/5712/fid/53
---
How do I execute an application on the client machine - taking into 
account the security issues?
http://www.faqts.com/knowledge_base/view.phtml/aid/1633]
---
Thank for script code am aready in use!
http://siobaranaska.spaces.live.com/
----------------------------------------------------------------------