0 Favourites

How do I read from the External Js file?

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

  • 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:

    "var js = document.createElement('script');
    js.type = 'text/javascript'; 
    js.src = 'chipmunk.js';
    document.body.appendChild(js);"[/code:300sntdv]
    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.
  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • 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

    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");
     }
    
    }
    
    [/code:1udlmti1]
    
    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!
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)