Jump to content

Load JS files only if they exist


Flink
 Share

Recommended Posts

Hello there dear people of this awesome forum!

 

I am hoping you can help me with a problem I have:

Let's say my game has these files:

  • main.js
  • player.js
  • hud.js

If hud.js is missing the game won't start. I need a way, so that this would only not show the hud but still execute the rest of the game.

Any tips are appreciated, thanks in advance!

Link to comment
Share on other sites

The only real way to do this would be to use something like requireJS, but even then this is a very strange requirement. Whatever your reason for maybe having a missing file, I would try my best to find an alternative / more sensible solution.

Link to comment
Share on other sites

Make an ajax request to your file, looking something like this:

$.ajax(
{  url: "./js/hud.js",

   success: function(result){
      console.log("file is there");
   },
   error: function(){
      console.log("file is not there");
   },
   type:GET,

   dataType:"text"
});


 

Link to comment
Share on other sites

Thanks for the answers. I will look into Ajax.

I am trying to build a programming learning environment. The player types some code in a web based IDE. These produced files shall then land in the game. So the player basically extends a basic game. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...