Did you ever want to run an application from your local resource inside your HTML page? If so, you will face some issues. It will never run any application from local resource directly. It may ask you to download the file before running it. This is due to browser security issue as the code runs inside the browser sandbox.

 

So, what to do for this? In this post, I will describe you the steps resolve to do this. Read the complete post in order to learn the same.

 

As I mentioned above, you can’t run application from browser window due to security reasons. To execute file you need to use .HTA applications (which is a HTML Application). HTA applications run outside the browser window just like a normal application and have full trust support.

 

If you are new to Html Application, read more about it here: http://msdn.microsoft.com/en-us/library/ms536471(v=VS.85).aspx

 

Let’s jump to create one Html Application to launch the notepad for you (without any security warnings). To do this, design your Html page with a button, which will execute the notepad once clicked. You can set some application specific properties using the <HTA:APPLICATION /> tag. This step is optional. Add the following JavaScript code inside your <head /> tag:

 

 
<script type="text/javascript" language="javascript">
    function RunFile() {
    WshShell = new ActiveXObject("WScript.Shell");
    WshShell.Run("c:/windows/system32/notepad.exe", 1, false);
    }
</script>

 

Here you can see that, first I am creating the instance of the “WScript.Shell” ActiveXObject and then calling the Run() method of the newly created object with proper parameters.

 

Now, from the button click event give a call to the JavaScript method named “RunFile()”. Once you click on the button, it will execute the file mentioned in the Run() method.

 

If you run this code inside the browser, it will not work. To resolve this, just save the file with a .hta extension. You will see that, the file icon has changed to application icon. If you double click on this file, it will execute and open a Window having the button inside it. Click the button to open the notepad.exe file.

 

Here is the full code for that:

 

 
<html>
<head>
    <title>Application Executer</title>
    <HTA:APPLICATION ID="oMyApp" 
        APPLICATIONNAME="Application Executer" 
        BORDER="no"
        CAPTION="no"
        SHOWINTASKBAR="yes"
        SINGLEINSTANCE="yes"
        SYSMENU="yes"
        SCROLL="no"
        WINDOWSTATE="normal">
    <script type="text/javascript" language="javascript">
        function RunFile() {
        WshShell = new ActiveXObject("WScript.Shell");
        WshShell.Run("c:/windows/system32/notepad.exe", 1, false);
        }
    </script>
</head>
<body>
    <input type="button" value="Run Notepad" onclick="RunFile();"/>
</body>
</html>

 

 

So, what is the purpose of it? You may need it in various reasons but that will depend on your requirement. Recently it was needed for me to do one on going R&D and this concept helped me a lot. Hence thought to share this with you, so that, you can also learn & use it in your requirement.


Have a question? Or, a comment? Let's Discuss it below...

dhgate

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.