javascript 在 HTML 中加载 Js 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15892815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
load Js file in HTML
提问by Thuong Tran
My first PhoneGap application includes 2 HTML files.
我的第一个 PhoneGap 应用程序包含 2 个 HTML 文件。
The first one is named index.html
which uses index.js
. This file will display a list item. When I click an item in that list, it brings me to detail.html
file by this:
第一个被命名index.html
,它使用index.js
. 该文件将显示一个列表项。当我单击该列表中的一个项目时,它会detail.html
通过以下方式将我归档:
$.mobile.changePage("detail.html", { transition: "slideup"});
OR
或者
location.href = "detail.html";
On the detail.html
page, I load detal.js
. However it did not work. I could not use functions in detail.js
.
在detail.html
页面上,我加载detal.js
. 然而它没有用。我无法在detail.js
.
Please give me your advise and there are any example ?
请给我你的建议,有什么例子吗?
Capture Photo
<link rel="stylesheet" href="css/jquery.mobile.structure-1.3.0.min.css" /> <link rel="stylesheet" href="css/jquery.mobile-1.3.0.min.css" /> <link rel="stylesheet" href="css/getAbstract.min.css" /> <script src="js/jquery-1.9.0.min.js"></script> <script src="js/jquery.mobile-1.3.0.min.js"></script> <script src="js/jquery.ba-dotimeout.js"></script> <script src="js/jquery.dst.js"></script> <script type="text/javascript" charset="utf-8" src="js/cordova-2.5.0.js"></script> <script type="text/javascript" charset="utf-8"></script> </head> <body> <div data-role = "page" data-theme = "a" id = "pageContainer"> <!--Start Page Header --> <div data-role = "header" id = "pageHeader" data-nobackbtn = "true" data-position = "fixed"> <h1>Camera</h1> </div> <!--End Page Header --> <!--Start Page Content--> <div data-role = "content" id= "pageContent"> <a data-role = "button" id = "btnCaptureEdit" href = "">Capture Edit</a> <a data-role = "button" id = "btnLibraryPhoto" href = "">Get Photo From Library</a> <a data-role = "button" id = "btnAlbumPhoto" href = "">Get Photo From Album</a> </div> <!--End Page Content--> <!--Start Page Footer--> <div data-role = "footer" id = "pageFooter"> </div> <!--End Page Footer--> </div> <script src="js/detail.js"></script> </body> </html>
拍摄照片
<link rel="stylesheet" href="css/jquery.mobile.structure-1.3.0.min.css" /> <link rel="stylesheet" href="css/jquery.mobile-1.3.0.min.css" /> <link rel="stylesheet" href="css/getAbstract.min.css" /> <script src="js/jquery-1.9.0.min.js"></script> <script src="js/jquery.mobile-1.3.0.min.js"></script> <script src="js/jquery.ba-dotimeout.js"></script> <script src="js/jquery.dst.js"></script> <script type="text/javascript" charset="utf-8" src="js/cordova-2.5.0.js"></script> <script type="text/javascript" charset="utf-8"></script> </head> <body> <div data-role = "page" data-theme = "a" id = "pageContainer"> <!--Start Page Header --> <div data-role = "header" id = "pageHeader" data-nobackbtn = "true" data-position = "fixed"> <h1>Camera</h1> </div> <!--End Page Header --> <!--Start Page Content--> <div data-role = "content" id= "pageContent"> <a data-role = "button" id = "btnCaptureEdit" href = "">Capture Edit</a> <a data-role = "button" id = "btnLibraryPhoto" href = "">Get Photo From Library</a> <a data-role = "button" id = "btnAlbumPhoto" href = "">Get Photo From Album</a> </div> <!--End Page Content--> <!--Start Page Footer--> <div data-role = "footer" id = "pageFooter"> </div> <!--End Page Footer--> </div> <script src="js/detail.js"></script> </body> </html>
回答by Ilia Frenkel
If this is your detail.html
I don't see where do you load detail.js
?
Maybe this
如果这是你的,detail.html
我不知道你在哪里加载detail.js
?也许这
<script src="js/index.js"></script>
should be this
应该是这个
<script src="js/detail.js"></script>
?
?
回答by user10654908
I had the same problem, and found the answer. If you use node.js with express, you need to give it its own function in order for the js file to be reached. For example:
我遇到了同样的问题,并找到了答案。如果你将 node.js 与 express 一起使用,你需要给它自己的函数才能访问 js 文件。例如:
const script = path.join(__dirname, 'script.js');
const server = express().get('/', (req, res) => res.sendFile(script))