Getting a SyntaxError when loading API

90 Views Asked by At

I load kongregate_api script in the head of the html document like this

<script src='https://cdn1.kongregate.com/javascripts/kongregate_api.js'></script>

Then loading my main.js script that contains these lines

    console.log(window.kongregateAPI)
    window.kongregateAPI.loadAPI(function () {});

screenshot of a part of main.js

The error message;

SyntaxError: An invalid or illegal string was specified in kongregate_api.js:36

console.log returns and object so i think it loads the script successfully.

screenshot of console logs

1

There are 1 best solutions below

1
On

Loading the API

In order to load the Kongregate API object, you need to include a script tag which loads our JavaScript source file. The script tag should be placed inside the head section of your document.

HTML

<script src='https://cdn1.kongregate.com/javascripts/kongregate_api.js'></script>

Initializing the API

The JavaScript API automatically creates a global variable named kongregateAPI. You can use this object to initialize the Kongregate API services with the loadAPI and the getAPI functions:

Example: Load the API, and set up a global reference to the API object named kongregate function once initialized.

JAVASCRIPT

kongregateAPI.loadAPI(function(){
  window.kongregate = kongregateAPI.getAPI();
  // You can now access the Kongregate API with:
  // kongregate.services.getUsername(), etc
  // Proceed with loading your game...
});

Note The API should only be loaded/initialized once per Kongregate game page load. If your application spans multiple pages/documents, please see the documentation on the Kongregate Shell for information on setting up and accessing a persistent API connection