【问题标题】:Getting error -> Unexpected token }出现错误 -> 意外令牌}
【发布时间】:2013-03-04 15:54:10
【问题描述】:

我收到标题中所述的错误。

这里是html代码

<!DOCTYPE HTML>
<html>
    <head>
        <title> PhoneGap </title>
        <script type="text/javascript" charset="utf-8" src="cordova-2.3.0.js"></script>
        <script type="text/javascript" charset="utf-8" src="fileopener.js"></script>
    </head>
    <body>
         <h1>PDF Open Tester</h1>
         <a href="#" onclick="window.plugins.fileOpener.open("/sdcard/Course Content Files/1.pdf");">open</a>
    </body>
</html>

这里是fileopener.js

function FileOpener() {
};

FileOpener.prototype.open = function(url) {
    cordova.exec(null, null, "FileOpener", "openFile", [url]);
};

if(!window.plugins) {
    window.plugins = {};
}
if (!window.plugins.fileOpener) {
    window.plugins.fileOpener = new FileOpener();
}

什么是错误?我的假设是错误不在cordova javascript文件中。

编辑来自 cmets:

我也得到了进一步的错误

Uncaught TypeError: Cannot read property 'fileOpener' of undefined.

【问题讨论】:

  • 我找不到错误。我认为错误在其他地方。
  • 您需要对onclick属性中的引号进行转义,或者在.open('...')内的内部对上使用单引号
  • 您还需要什么其他信息?你可以参考这个问题,因为它有点相关,stackoverflow.com/questions/15203770/…
  • @Michael,解决了问题。
  • 如何将迈克尔的答案设为正确答案?

标签: javascript android html cordova syntax


【解决方案1】:

应 OP 的要求移植评论以回答:

HTML 标记中的引用错误。 onclick 属性的外引号是双引号,但在 .open() 方法调用内的字符串上也使用了双引号。将一个或另一个更改为单引号。由于您已经在 HTML 属性中使用双引号,因此最好在方法调用字符串中使用单引号。

<a href="#" onclick="window.plugins.fileOpener.open('/sdcard/Course Content Files/1.pdf');">open</a>

最好的策略是避免将onclick 完全放在标记中,而是使用适合PhoneGap 应用程序的任何方法将事件绑定到代码中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多