How do I read from the External Js file?

Get help using Construct 2

Post » Wed Nov 11, 2015 8:35 pm

Hi Guys, i'm spending a lot of time in searching how to get js data from the external js file, but no luck.

I have added *.js file to the project folder, and i need to pick a function from this file.
So, if this function or value inside of it is true(or exist), i'm switching my value inside a Construct and executing new js

Is anybody know how to do that? Or maybe you can point me to the existing tutorials that i missed.

Many thanks!
Image
B
13
S
4
Posts: 13
Reputation: 853

Post » Wed Nov 11, 2015 10:23 pm

Ideally you'd use the plugin sdk to access javascript.

But you can use the Browser object with it's exec js action to run some javascript.

You can load a js file in your project folder as if it was included in your html file by executing this:
Code: Select all
"var js = document.createElement('script');
js.type = 'text/javascript';
js.src = 'chipmunk.js';
document.body.appendChild(js);"

The library takes time to load though, so you can't use it immediately.

Another option is to load the the file into a variable with the ajax plugin. Then you can use exec js with that.
B
94
S
33
G
117
Posts: 5,388
Reputation: 75,337

Post » Sat Nov 14, 2015 12:12 pm

Thanks for the answer!

I found an and easy way how to do that:
To hear from the index.html file i put some code to it.
i need index.html file to read from the linked js file in the game folder
We have to know ads availability to make an action in construct .

That code was pasted to the end of the index.html file.
Inside the bottom <script></script> brackets
Code: Select all

function checkForAd()
{
 //make sure webstorage is supported
 if (typeof (Storage) !== "undefined")
 {
     //check if the key exists
     if (//type your funtion from any of the js files you have in your project) {
        console.log("Ad is available; localforage is " + localforage);
         localforage.setItem("adAvailable", "yes", function(){
            console.log("item has set to: yes");
         });
     }
     else {
        console.log("Ad is NOT available; localforage is " + localforage);
         localforage.setItem("adAvailable", "no", function(){
            console.log("item has set to: no");
         });

    }

    console.log("localforage.adAvailable=" + localforage.getItem("adAvailable"));
 } else {
    console.log("Storage is undefined");
 }

}



In the construct i made some actions:

Every level i need to call this script to not left it sleep ( just to be sure it works), by Executing javascript: "checkForAd();"
Each time we loose the game we checks the availability of the local storage key : LocalStorage- check item exist: "adAvailable"
On iten "adAvailable" exist we are getting it: LocalStorage get item: "adAvailable"
And then, on item Get, we can compare it's key or just do our action we plan, for me it to create a button.

So, i hope it can help you to hear from the html file or js file any function you need.


Peace!
Image
B
13
S
4
Posts: 13
Reputation: 853


Return to How do I....?

Who is online

Users browsing this forum: dop2000, jatin1726 and 3 guests