Veda Sadhak
Veda Sadhak

Reputation: 424

Getting error -> Unexpected token }

I am getting the error as stated in the title.

Here is the html code

<!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>

Here is the 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();
}

What is the error? My assumption is that the error is not in the cordova javascript file.

Edit From comments:

I'm also getting a further error

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

Upvotes: 1

Views: 619

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270617

Porting comment to answer at OP's request:

The quoting in the HTML markup is faulty. The onclick attribute's outer quotes are double, but double quotes have also been used on a string inside the .open() method call. Change one or the other to single quotes. Since you are already using double quotes for HTML attributes, probably it is best to use single inside the method call string.

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

The best strategy would be to avoid placing the onclick inside the markup entirely, and instead bind the event in code using whatever method is appropriate for PhoneGap applications.

Upvotes: 4

Related Questions