• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 177
  • Last Modified:

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.
0
hieukhtn
Asked:
hieukhtn
  • 5
  • 4
1 Solution
 
ZvonkoSystems architectCommented:
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
}

0
 
hieukhtnAuthor Commented:
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
}
0
 
hieukhtnAuthor Commented:
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

----------------------------
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ZvonkoSystems architectCommented:
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()
}



0
 
hieukhtnAuthor Commented:
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
0
 
ZvonkoSystems architectCommented:
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
}


0
 
ZvonkoSystems architectCommented:
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(;-)



0
 
hieukhtnAuthor Commented:
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
0
 
ZvonkoSystems architectCommented:
OK, sorry, I assumed that.
Complete example is this:
//JS2.js
document.write('<scr'+'ipt src="JS1.js"> </scr'+'ipt>');
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()
}

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now