Flink

Load JS files only if they exist

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!

Share this post


Link to post
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.

Share this post


Link to post
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"
});


 

Share this post


Link to post
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. 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.