2

I have the following snippet of the code:

<html>

<head>
<title>Example of Very First PHP Script ever!</title>
</head>

<body>
<script>
function comexe(){
C:\Program Files\Notepad++\notepad++.exe C:\Documents and Settings\User\Desktop\file.txt
}
</script>

<a href="return false;" onclick="comexe()">music</a>

</body>
</html>

How can I run another program from the HTML? What am I doing wrong?

pimvdb
  • 151,816
  • 78
  • 307
  • 352
geek
  • 2,677
  • 4
  • 23
  • 21
  • 8
    You can't - HTML is client side, and javascript is sandboxed to protect the client PC from exactly this. Apart from the title, your script has nothing to do with PHP - which can run a filesystem command, but only on the server. – Adam Hopkinson Sep 14 '11 at 11:30
  • @adam: why don't you add this as an answer? Then we can upvote it. – Kees de Kooter Sep 14 '11 at 11:32
  • Imagine that every page would be able to run something on your computer when you load it in your browser. Al Gore wouldn't be as famous as he is now. ... @Kees de Kooter seems to trivial to add it as an answer (no offense to Pranay) – ZenMaster Sep 14 '11 at 11:32
  • +1 @adam. what are you trying to do is right opposite what browsers trying to prevent :) – haynar Sep 14 '11 at 11:33
  • Thanks all, I've added my comment as an answer – Adam Hopkinson Sep 14 '11 at 11:34

4 Answers4

9

Adding this as an answer, as requested in the comments

You can't - HTML is client side, and javascript is sandboxed to protect the client PC from exactly this. Apart from the title, your script has nothing to do with PHP - which can run a filesystem command, but only on the server.

Adam Hopkinson
  • 28,281
  • 7
  • 65
  • 99
  • It is possible to launch a `.exe` from a web page using a [custom URL protocol](https://stackoverflow.com/questions/80650/how-do-i-register-a-custom-url-protocol-in-windows). – Anderson Green May 05 '22 at 23:19
2

You can not do that becasue html and javascript both client side tech. and have restriction so that it can not access resource of the client machine.

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
2

Technically you could execute a program from inline vbscript. You would normally do this in a .hta file running locally.

Something like:

<html>

<head>
<title>Example of Very First PHP Script ever!</title>
</head>

<body>
<script language="VBScript"> 

Sub RunProgram 
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run "C:\Program Files\Notepad++\notepad++.exe C:\Documents and Settings\User\Desktop\file.txt"
End Sub

</script>

<a href="return false;" onclick="RunProgram">music</a>

</body>
</html>
James Santiago
  • 2,883
  • 4
  • 22
  • 29
0

If it's a operation that you have to perform many times on client machine this could be achieved. Just install a web server (wamp or xamp or what you want on machine) and from your page call

http://localhost/nameofscript.php

this script can execute whatever you want if you set correct permissions on the user of local webserver

Perhaps, if is one-time operation that a client must execute is not so pratic to install a webserver only for an onetime operation

Bertoli Stefano