Link to home
Start Free TrialLog in
Avatar of hieukhtn
hieukhtn

asked on

Special include javascript file?!

Hello all!

If I has 2 Javascript file name: "JS1.js" and "JS2.js"
And the JS2.js need some resource from JS1.js, and I don't want to use HTML to include JS1.js first and after that JS2.js because incase we have so many dependency like this, we can't control each include.

So, is there any solution for this problem?

Actually, I has used some solution like:
       document.write('<script src="JS1.js"> </script>');
But It isn't worked, because in this case, the JS1.js has been loaded after the JS2.js.

Thanks for reading and giving me some advise.
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

The loading sequence should never be the problem for any function either from js2 functions using parts from js1 or vica versa.
If you have dependancies then set global vars that marks one package loading finished. For example, set in the js1 some global var outside of any function like this:
var js1Loaded = true;

in your js2 calls you can check for that var and restart in 100 miliseconds as long as that var is not true:

like this:

function callJs1Func(){
  if(!js1Loaded){
     setTimeout("callJs1Func()",100);
     return
  }
  // do the calls and other stuff here
}

Avatar of hieukhtn
hieukhtn

ASKER

Can you explain more about that?
Because I don't understand why JS2 can call JS1 script in this case. Because why it know the exists of JS1 and what is in side of JS1. (in JS2 should have some script such as include(JS1)) is that true?


Suppose that we have
JS1.js
----------------
function test() {
   alert("this is a function of JS1.js");
}

------------------
JS2.js
function test2(){
          // do something

         test(); // call test of JS1

         ///// do some thing else
}
Sorry! I make some mistake in the example above.
JS2.js must be:
---------------------------
// do something

test(); // call test of JS1 - call directly without inside a function

///// do some thing else

----------------------------
Like this:

JS1.js
----------------
function test() {
   alert("this is a function of JS1.js");
}
var js1Loaded = true;
------------------
JS2.js
function test2(){
          // do something

         test(); // call test of JS1

         ///// do some thing else
}

function callJs1Func(){
  if(!js1Loaded){
     setTimeout("callJs1Func()",100);
     return
  }
  test2();
}

If you do not want to change the js1.js library, the you can test for functions loding directly like this:
JS2.js
function test2(){
          // do something

         test(); // call test of JS1

         ///// do some thing else
}

function callJs1Func(){
  if(! test){ // test is the exact name of js1 defined function but without () braces
     setTimeout("callJs1Func()",100);
     return
  }
  test2(); // now is test() loaded so you can call your test2()
}



Finally I understood what you mean but I think you misunderstood me.
Cause I want, in the HTML page that use JS2, I just want to include JS2 or just one more library JS file.

Ex: we have more JS file mean library.js to do some very common function or help others.
And we have 1 HTML that need JS2.js.

I want the html page must be:
----------------------------------------------------
<!-- may be need library.js
<script src="libary.js"></script>
-->

<script src="JS2.js"> </script>
-------------------------------------------------------
html page don't call JS1.js so that your solution isn't work anymore.

So please give me another solution. Thanks
What the most programmer do not see is: there is NO way to wait in JavaScript.
That does mean this can never work:

function test2(){
          // do something
   if(!test) {
     // loop or wait or sleep is not possible or would in loop case block the loading of test()
  } else {
         test(); // call test of JS1
  }
         ///// do some thing else
}


Solution for what? I think you are loading your js1.js from js2.js by doing a document.write() in the js1.js definition? why do you want to change that? Or I do not understand your question.

If I understood you then your question is: why is the world not perfect and js1.js is not included when I need it in js2.js?
The answer is: that is all possible when you implement this function with exactly following syntax: doWhatImean(;-)



calm down! :)
This is yours: "I think you are loading your js1.js from js2.js by doing a document.write() in the js1.js definition?"
And in your answer, I can't find out any document.write(); so that I wonder why it could happen?

May be I'm not good at JS, I'm a newbie. So please let me know what exactly you mean?

Thank you so much. :-D
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial