如何处理多个 Javascript 文件
我有一个 PhoneGap HTML5 文件,如下所示(仅显示头部的一部分),
<head>
<script type="text/javascript" src="Javascript/phonegap-1.3.0.js"></script>
<!--<script type="text/javascript" src="Javascript/photo.js"></script>-->
<script>
function takePhoto()
{
alert("Take Photo");
navigator.camera.getPicture(onSuccess, onFail, {
quality: 50,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.CAMERA});
}
function onSuccess(imageData)
{
var image = document.getElementById('photoImg');
image.src = "data:image/jpeg;base64," + imageData;
}
function onFail(message)
{
alert('Failed because: ' + message);
}
</script>
</head>
在正文中我有这个标记,
<button onclick='takePhoto()'>Take Photo</button>
像上面那样定义它就可以了。问题是我不想在我的 html 文档中定义 javascript。我希望它看起来像这样,
<script type="text/javascript" src="Javascript/phonegap-1.3.0.js"></script>
<script type="text/javascript" src="Javascript/photo.js"></script>
并且让 photo.js 包含 takePhoto 方法。我无法让它像那样工作。在我的 javascript 控制台中我收到错误,
Uncaught ReferenceError: takePhoto is not defined at file:///android_asset/www/index.html:81
I have a PhoneGap HTML5 file as follows (only showing part of the head),
<head>
<script type="text/javascript" src="Javascript/phonegap-1.3.0.js"></script>
<!--<script type="text/javascript" src="Javascript/photo.js"></script>-->
<script>
function takePhoto()
{
alert("Take Photo");
navigator.camera.getPicture(onSuccess, onFail, {
quality: 50,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.CAMERA});
}
function onSuccess(imageData)
{
var image = document.getElementById('photoImg');
image.src = "data:image/jpeg;base64," + imageData;
}
function onFail(message)
{
alert('Failed because: ' + message);
}
</script>
</head>
In the body I have this markup,
<button onclick='takePhoto()'>Take Photo</button>
Having it defined like above works fine. The problem is that I don't want to define the javascript in my html document. I want it to look like this,
<script type="text/javascript" src="Javascript/phonegap-1.3.0.js"></script>
<script type="text/javascript" src="Javascript/photo.js"></script>
And have photo.js contain the takePhoto method. I cannot get it to work like that. In my javascript console I get the error,
Uncaught ReferenceError: takePhoto is not defined at file:///android_asset/www/index.html:81
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,我遇到的主要问题是我的 photo.js 文件中有错误。修复错误让我到达了它可以工作的地方。
我认为这个问题还有更多内容,这就是我在这里问问题的原因。之前,当我内联脚本时,它也不起作用,让我认为那里的脚本无法调用phonegap.cs中的代码。
事实证明,它在我的电脑上的浏览器中不起作用,但在实际的 Android 和 Android 模拟器中它可以工作。
OK, so the main problem I was having there was that my photo.js file had an error in it. Fixing the error got me to a place where it was working.
I thought there was more to this issue though which is why I asked a question here. Before when I had the script inline it wasn't working either, making me think that the script there was not able to call the code in phonegap.cs.
Turns out that it doesn't work in a browser on my PC, but on an actual android and in the android simulator it is working.
检查一下:这是您导入的 file.js
然后您的 html 文档可以执行类似的操作
Check it : Here is your file.js your importing
Then your html document can do something like