Reputation: 5368
I have a hyperlink in my html document as
<a href="file://c:/directory/file.txt">click</a>
When I click on the hyperlink, the specified file opens in the browser itself.
Is it possible to open that file in notepad or sublime text or any text editor which is the default program for the file type (txt in this case). Can I do it programmatically in javascript or jquery. Is there any way to do it?
Note: This is just a stand alone application. I have developed a plugin which searches for patterns, that I have given as input, in all the files in a given source directory and print out all the file names in that directory which contains that pattern. Its printed to a output report html file. I just want to link the file path name to the default editor, so that he can edit it and save. No issue of security. Can it be done?
If not, is there any other way to accomplish this task? I can generate the output report in any format. Pls help.
Upvotes: 2
Views: 4016
Reputation: 553
I was looking for something similar and found a solution. Maybe someone will find it useful. If you only want to do this on your own computer, it works.
Upvotes: 0
Reputation:
As others mentioned, you may not able to do this without compromising your browser's security. But I came across a link that may help you.
<script type="text/javascript" language="javascript">
function RunFile() {
WshShell = new ActiveXObject("WScript.Shell");
WshShell.Run("c:/windows/system32/notepad.exe", 1, false);
}
</script>
Edit: This opens an empty notepad, and it'll only work on windows. You may use js
to distinct between different OS and run their default text editors accordingly, but I wouldn't on that route. Why not use online editors instead?
Upvotes: 3
Reputation: 122006
Due to security reasons HTML/JavaScript
does'nt have allow to access local file system.
http://en.wikipedia.org/wiki/JavaScript#Security
JavaScript and the DOM provide the potential for malicious authors to deliver scripts to run on a client computer via the web. Browser authors contain this risk using two restrictions. First, scripts run in a sandbox in which they can only perform web-related actions, not general-purpose programming tasks like creating files. Second, scripts are constrained by the same origin policy: scripts from one web site do not have access to information such as usernames, passwords, or cookies sent to another site
You have to make a server request in order to get the resource.
Upvotes: 2
Reputation: 12243
You can't do this without compromising the browser's security. You'd have to modify the browsers MIME type for TXT and default program's associated. If it's on your local network, and you're on a domain, and each user has admin privileges on their machines, you could theoretically do a registry hack and change the default settings for opening text files in external editors, but this compromises security, and chances are if you're in a domain network, your admin privileges are stripped from local machines.
Upvotes: 1