如何处理多个 Javascript 文件

发布于 2024-12-28 10:13:22 字数 1476 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染墨丶若流云 2025-01-04 10:13:22

好的,我遇到的主要问题是我的 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.

口干舌燥 2025-01-04 10:13:22

检查一下:这是您导入的 file.js

// I changed it for the purposes of not intersecting any other library.
(function() {
    MyLibrary = {
        takePhoto: function() {
            // your code here
        }
    }
})()

然后您的 html 文档可以执行类似的操作

<button onclick='MyLibrary.takePhoto()'>Take Photo</button>

Check it : Here is your file.js your importing

// I changed it for the purposes of not intersecting any other library.
(function() {
    MyLibrary = {
        takePhoto: function() {
            // your code here
        }
    }
})()

Then your html document can do something like

<button onclick='MyLibrary.takePhoto()'>Take Photo</button>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文