0

I making a game with few scenes. Each scene have it's own javascript which i want to load asynchronously and after that to load the particular scene file into div.

Here is my html index.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="http://code.jquery.com/jquery-1.9.1.js"> </script>

    <script type="text/javascript" src="first.js" id="first" ></script>
</head>
<body>

<div id="test" style="width: 800px; height: 400px; background: black">
    hello

</div>
                                    <button id="submit">submit</button>

</body>
</html>

Here is two working functions.

First one

$(document).ready(function() {

    $('#submit').click(function() {


        (function() {
           var myscript = document.createElement('script');
            myscript.type = 'text/javascript';
            myscript.src = ('second.js');
            var s = document.getElementById('first');
            s.parentNode.insertBefore(myscript, s);
        })();

    });

});

And here is the second

 $( "#test" ).load('div.html');

I want the second command: $( "#test" ).load('div.html'); to be executed straight after the scripts loading finished successfully.

How can i do that?

0

1 Answer 1

2

just put the another .load() to the call back of another .load() function

$( "#targetelement" ).load('myajaxpage.php', function(){
      //call back
       $( "#targetelement" ).load('myajaxpage.html', function(){
             //call back
      }) 
});

as I realized the the title of your question is different on what you are asking which is

I want the second command: $( "#test" ).load('div.html'); to be executed straight after the scripts loading finished successfully.

you need to use a script loader that have callback functionality like the following script loaders below

http://headjs.com/

head.js("/path/to/jquery.js", "/google/analytics.js", "/js/site.js", function() {

   // your function you want to call after the scripts above is loaded

});

http://yepnopejs.com/

yepnope.injectJs("jquery.js", function () {
  // your function you want to call after the scripts above is loaded

}, {
  charset: "utf-8"
}, 5000);

well there are a lot of script loaders but the two above in my experience were really good for me

Sign up to request clarification or add additional context in comments.

3 Comments

That is not an answer to my question
Actually i want to load some scripts for the scene1, and after i go to the scene2 i want to replace those scripts with others. So i have to give them an id or something (in order to replace them after) So i prefer to use the function that i wrote. Or is it any ability to replace scripts with head.js ??
well you need to try it by yourself, you can replace scripts but replacing the variables and objects stored in the memory that came from the previous scripts cannot be replaced without manually doing it in code.. you need to put them in a single namespace so if you changed that namespace everything will be replaced (like what jquery does with their single common namespace which is the $)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.