Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

include a .js in a .js ??? Vituz

Status
Not open for further replies.

MikeDee

Programmer
Aug 2, 2001
19
GB
You know how you can

<script src=&quot;file.js&quot;></script>

and include a .js in an HTML Page.

Ive tried the same in a .js file and it doesnt work. So I was wondering is there a way to, when you're in a .js to include another .js?

-------------
Anybody,

How can I call a .js file in a .js file?

ie. file.js (full of functions) call filearray.js (arrays)?
 
what is ur point?
as i wrote before, just write in ur html document
<script src=&quot;file.js&quot;></script>
<script src=&quot;filearray.js&quot;></script>

or if u wanna include it from js file (note: it wuld be included into
HTML:
 file, not in js):
document.write(&quot;<script src=\&quot;filearray.js\&quot;></scr[COLOR=red][b]&quot;+&quot;[/b][/color]ipt>&quot;)
be shure 2 write it just like i did (i mean red) or else u'll get an error..
 regards, vic
 
MikeDee,
The nature of
<script src=&quot;file.js&quot;></script>
is that in its place in html file code form file.js is included.
As Vic is explaining, you could have 2 (or more) diffrent external js files, and when you include them in html file, all the code is available to you, so you could read the array values and use them in your functions.
If you for some reason want to have only one included js file, and all needed to be included into it - answer is no, it is not possible, as far as I know.
D.
 
But could a function in js_include_1.js call a function
in js_include_2.js? I am not able to get this to work, I can have a button's onclick event call a function in
js_include_1.js, which could call another function in
js_include_1.sj which isnt what I want (and its a little
redundant ;).

I have a .js that is specific to my webpage (checks
controls that are known to be on that page), and the other
.js file (#2) has a generic library of sorts for checking
things like valid email addresses, etc. I need the page
specific code to call the general-use functions in the
other, but its not working.

Is there something like a #include (c-like) for Javascript
that I should be putting into the .js files themselves?
Or is this something that shouldnt be an issue because it
should work as is (if done correctly)?

Regards,
Tj
 
Just encountered the same problem.

I came up with this (seems to work)

suppose you have a.js and b.js

so in a.js you can put this line:

document.write('<script language=&quot;JavaScript1.2&quot; src=&quot;b.js&quot;> </script>')

b.js is now also included and accessible to functions in a.js

Ron
 
I did end up being able to get this work as you should (just including both, and calling func one from within func two)...

see thread id: Thread216-405070
 
Happy New Year guyz 'n girlz, but i would turn the notification off :)
 
BigTeeJay, how did you do this?

Does anyone know how he did that? I am having the same problem.
 
Blicarea,

it is perfectly legal to include two .js files on the same page, and have one call functions from another.

for example:

<script type=&quot;text/javascript&quot; src=&quot;a.js&quot; /></script>
<script type=&quot;text/javascript&quot; src=&quot;b.js&quot; /></script>

and have a function in a.js call a function in b.js.

if you are having problems with this, check to make sure there's no function name or var conflicts between the files, and that your paths are correct.


=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
I understand that you can link to two .js files from the same HTML page, but that's not what I'm trying to do. I'm trying to add a google adsense feature (a .js link) from an existing .js file that is linked to from every page on my site. BigTeeJay said he did this by &quot;just including both, and calling func one from within func two)...&quot;

Thanks
 
Blicarea,

i guess i'm not understanding your situation exactly, but what BigTeeJay says he did is just what i stated:

he's got a.js which has something like:
Code:
function inA(s) {
  if (s) s = &quot;\n\n&quot; + s;
  alert(&quot;i'm in a.js&quot; + s);
}

and b.js which has something like:
Code:
function inB(s) {
  inA(&quot;calling from b.js&quot;);
}

BigTeeJay is simply including both .js files on his page, and calling a function from b.js that is defined in a.js



=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
well, in your case you simply need to put this in the <head> of your document:
Code:
<script type=&quot;text/javascript&quot; src=&quot;4.js&quot; /></script>
<script type=&quot;text/javascript&quot; src=&quot;[URL unfurl="true"]http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;[/URL] /></script>

are you having some kind of error when you do this?



=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
No, the google code needs to go inside 4.js.

Thanks for your help, btw.
 
ok - we said before that is not possible. javascript itself has no &quot;import&quot; statement...the only solution is to use HTML <script> tags to include multiple files on your page.

what BigTeeJay did and what i have been saying is that you can include multiple .js files on one page by virtue of the <script> tag, and then you will be able to call functions in any of the includes from anywhere else on the page.

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Please show me with examples, Jemminger. I have no idea what you're saying.
 
what i'm saying is this is not possible:
FIG.1
<html>
<head>
<script src=&quot;abc.js&quot;></script>
</head>
</html>

where &quot;abc.js&quot; is (pseudocode):
// begin file
magicallyInclude(a.js);
magicallyInclude(b.js);
magicallyInclude(c.js);

callFunctionInAdotjs();
callFunctionInBdotjs();
callFunctionInCdotjs();
// end file

the idea being that you could write one html script tag that would include ALL of your .js includes. this is not possible.


THIS is possible:
FIG.2
<html>
<head>
<script src=&quot;a.js&quot;></script>
<script src=&quot;b.js&quot;></script>
<script src=&quot;c.js&quot;></script>

<script>
callFunctionInAdotjs();
callFunctionInBdotjs();
callFunctionInBdotjs();
</script>

</head>
</html>


if i understand your intent correctly, you wish to simply include one file &quot;4.js&quot; and have 4.js pull in the google .js file as well. this cannot be done in real-time - i.e. you CAN do it via document.write, but your data in the &quot;included&quot; file will not be available until that file loads after the page onload event.

e.g.:
FIG.3
// begin 4.js
document.write('<scr'+'ipt type=&quot;text/javascript&quot; src=&quot;// ...the rest of 4.js follows...

// end 4.js

while it is perfectly legal syntax to do something like in FIG.3 above, the caveat is that everything within is UNAVAILABLE until after the page loads, and beyond that after googlesyndication's servers decide to serve it up.

so, it is possible with strings attached. perhaps this will work for you.


=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top