简体   繁体   English

如何通过按JavaScript / HTML中的按钮打开本地文件?

[英]How do I open a local file from a button press in JavaScript/HTML?

I am very new to JavaScript and HTML. 我对JavaScript和HTML非常陌生。 I created an example where im trying to open a local file called, "TNTplayer.html" in a JavaScript function via a button press. 我创建了一个示例,我尝试通过按下按钮在JavaScript函数中打开名为“ TNTplayer.html”的本地文件。 However, I don't know the line of code that will do that. 但是,我不知道能做到这一点的代码行。

 function myFunction() { document.location.href=document.location.href.splitOnLast('/')[0]+"/TNTplayer.html"; } 
 <p>Click the button.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> 

Have a look at this article: http://researchhubs.com/post/computing/javascript/open-a-local-file-with-javascript.html 看看这篇文章: http : //researchhubs.com/post/computing/javascript/open-a-local-file-with-javascript.html

In general, if your HTML/JS is being served as a website, you will not have "true" access to your filesystem (which is a necessary security measure), besides the ways mentioned in the article above. 通常,如果将HTML / JS用作网站,则除了上面文章中提到的方法之外,您将没有对文件系统的“真实”访问(这是必要的安全措施)。

However, if you are serving it as a standalone client application, which is possible using frameworks such as Nodejs and others, there are framework specific ways of accessing the filesystem. 但是,如果将其作为独立的客户端应用程序使用(可以使用诸如Node.js和其他框架之类的框架来实现),则可以使用特定于框架的访问文件系统的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何打开要在其中运行javascript的Ghost本地html文件? - How do I open a Ghost local html file in which I want to run javascript? 如何创建本地html文件以打开网页并注入JavaScript函数 - How do I create a Local html file to open webpage and inject a JavaScript function 当有人单击按钮时,如何打开JavaScript文件? - How do i open a javascript file, when someone clicks on a button? 如何从本地运行的 html+javascript 页面访问和读取本地文件 - How do I access and read a local file from html+javascript page running locally 如何在此JavaScript和HTML代码中添加按钮,以便在按下按钮时,交通信号灯序列运行一次? - How do i add a button to this JavaScript and HTML code so that when i press the button, my traffic light sequence runs once? 如何实现按住按钮 javascript? - How do I implement press and hold button javascript? 如何在javascript中打开HTML文件? - How do you open HTML file in javascript? 如何从HTML读取本地访问数据库文件? - How do I read a local access database file from HTML? 如何在本地 javascript 文件中使用来自 CDN 的 object? - How do I use an object from a CDN in a local javascript file? 如何通过单击HTML按钮执行JavaScript函数? - How do I execute a JavaScript function from clicking an HTML button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM