Connecting Tech Pros Worldwide Forums | Help | Site Map

including a javascript file in another.. HOW!?!?

Newbie
 
Join Date: Apr 2006
Posts: 7
#1: Apr 27 '06
how do you include a javascript file A in another javascript file B inorder to be able to use the functions in A from B.

i thought the code in javascript file B would be like this...

<script src="chrome://followmylink/contents/fileA.js">

...code in B where i will be using the functions from B ...

</script>

Also is there a way how you would define a class?

is it like this:

var ClassA = new Object {
//..attributes..
this.attr1 = "";
}

//functions
ClassA.method1 = function() {
//.....code......
}

If so how should I call it from file A if I put it in file B

Thanks very much!!!
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Mar 27 '08

re: including a javascript file in another.. HOW!?!?


Expand|Select|Wrap|Line Numbers
  1. var head = document.getElementsByTagName('head')[0];
  2. var script = document.createElement('script');
  3. script.setAttribute('type','text/javascript');
  4. script.setAttribute('src',nameOfFile);
  5. head.appendChild(script);
or using document.write (and escaping the ending script tag).
Expert
 
Join Date: Nov 2006
Posts: 392
#3: Mar 27 '08

re: including a javascript file in another.. HOW!?!?


Quote:

Originally Posted by broli85

how do you include a javascript file A in another javascript file B inorder to be able to use the functions in A from B.

i thought the code in javascript file B would be like this...

<script src="chrome://followmylink/contents/fileA.js">

...code in B where i will be using the functions from B ...

</script>

The code in the B file goes out side fo the script tag from the A file. See below.

Expand|Select|Wrap|Line Numbers
  1. <script src="chrome://followmylink/contents/fileA.js"></script>


Quote:

Originally Posted by broli85

Also is there a way how you would define a class?

Expand|Select|Wrap|Line Numbers
  1. <script>
  2.     function myClassFile(){
  3.  
  4.         this.someVar = 'This is a String';
  5.  
  6.         this.someMethod = new function(){
  7.             .....
  8.             .....
  9.         }
  10.     }
  11. <script/>
  12.  


Quote:

Originally Posted by broli85

If so how should I call it from file A if I put it in file B

Expand|Select|Wrap|Line Numbers
  1. <script>
  2.  
  3.     document.write('<script src="chrome://followmylink/contents/fileA.js"></script>');
  4.  
  5.  
  6.     var myClassInstance = new myClassFile();
  7.  
  8.     myClassInstance.someMethod();
  9.  
  10. <script/>
  11.  
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Mar 31 '08

re: including a javascript file in another.. HOW!?!?


Quote:

Originally Posted by pronerd

Expand|Select|Wrap|Line Numbers
  1. <script>
  2.  
  3.     document.write('<script src="chrome://followmylink/contents/fileA.js"></script>');
  4.  

Browsers may close the script on encountering "</script> within the script, so it should be escaped, e.g. <\/script>.
Reply