Hi all
Something I need help on.
I have several .js file and I was wondering if it was possible if I could call all of them with just one .js file.
thanks in advance
waiting for reply
8 2480
not sure what you mean by "call a file".
unlike compiled languages, javascript doesn't care where the source code comes from.
you can combine or detach code in any number of files.
keep in mind that some code has dependencies , so keep the code load order the same.
not sure what you mean by "call a file".
unlike compiled languages, javascript doesn't care where the source code comes from.
you can combine or detach code in any number of files.
keep in mind that some code has dependencies , so keep the code load order the same.
yes it is possible...
eg. save the following file as sample.js - <html>
-
<script type="javascript" src="1.js"></script>
-
<script type="javascript" src="2.js"></script>
-
<script type="javascript" src="3.js"></script>
-
</html>
-
now call this sample.js file in a html page
That's not JavaScript and the type attribute is incorrect.
If you want to include JavaScript files within a JavaScript file, use document.write, e.g. - document.write("<script type='text/javascript' src='f1.js'><\/script>");
If you want to include JavaScript files within a JavaScript file, use document.write, e.g. - document.write("<script type='text/javascript' src='f1.js'><\/script>");
if the page has completed, the above code would kill the page.
use a function to add other script after (or during for that matter) load. -
function addScript(url) {
-
var xJs = document.createElement("script");
-
xJs.type = "text/javascript";
-
var h = document.getElementsByTagName("head");
-
if (h && h[0]) {
-
h[0].appendChild(xJs);
-
}
-
xJs.src = url;
-
return xJs;
-
}
-
You're correct, though I assumed that this was to be an included JavaScript file in the head.
Thanks ...it is working now!
gr8
fyi...js files don't have to me loaded in the head, this code essentially works as an "include / require" script just save it as "sciptTest.js" or whatever, and you can actually just type the name of the javascript file into the textarea...Set overwrites any the file if it already exists, and add just adds it if it isn't already there...this is just a basic idea, you can obviously call this function from within other scripts.....also, as mentioned above, if you have functions in seperate scripts that work together, you must make sure you load the scripts in the right order or they won't work:): -
initer=function(){
-
GL.input=document.createElement('textarea');
-
document.body.appendChild(GL.input);
-
GL.input.width=150+'px';
-
GL.inputButt=document.createElement('div');
-
document.body.appendChild(GL.inputButt);
-
GL.inputButt.style.position='absolute';
-
GL.inputButt.style.top=GL.input.offsetTop+'px';
-
GL.inputButt.style.left=200+'px';
-
GL.inputButt.innerHTML='Add';
-
GL.inputButt.onmouseup=function(evt){loadScript(evt)};
-
GL.inputButt.switcher='Add';
-
GL.inputButt2=document.createElement('div');
-
document.body.appendChild(GL.inputButt2);
-
GL.inputButt2.style.position='absolute';
-
GL.inputButt2.style.top=(GL.inputButt.offsetTop+GL.inputButt.offsetHeight)+'px';
-
GL.inputButt2.style.left=200+'px';
-
GL.inputButt2.innerHTML='Set';
-
GL.inputButt2.onmouseup=function(evt){loadScript(evt)};
-
GL.inputButt2.switcher='Set';
-
GL.sDiv=document.createElement('div');
-
document.body.appendChild(GL.sDiv);
-
GL.docBod=document.createElement('div');
-
document.body.appendChild(GL.docBod);
-
}
-
loadScript=function(evt,switcher){
-
var target=switcher?false:GL.getTarget(evt);
-
var switcher=switcher || target.switcher;
-
GL.nScript=GL.nScript || new Object();
-
switch(switcher){
-
case 'Set':
-
GL.sDiv.parentNode.removeChild(GL.sDiv);
-
GL.sDiv=document.createElement('div');
-
document.body.appendChild(GL.sDiv);
-
document.body.removeChild(GL.docBod);
-
GL.docBod=document.createElement('div');
-
document.body.appendChild(GL.docBod);
-
setTimeout("init()",500);
-
case 'Add':
-
if(GL.nScript[GL.input.value]){GL.nScript[GL.input.value].parentNode.removeChild(GL.nScript[GL.input.value]);GL.nScript[GL.input.value]=false};
-
GL.nScript[GL.input.value]=document.createElement('script');
-
GL.nScript[GL.input.value].type='text/javascript';
-
GL.nScript[GL.input.value].src=GL.input.value+'.js';
-
GL.sDiv.appendChild(GL.nScript[GL.input.value]);
-
}
-
init=null;
-
return (GL.nScript[GL.input.value]);
-
}
P.S. in this snippet, "Set" also fires "init". So if you want to load a file and initialize it, that works nicely, remember that when you load a new script, any functions that are named like existing functions will be overwritten
I know this seems resolved, but I had a project I needed my script loader for, and I suddenly realized it wasn't what I thought it was LoL so here is my actual loader, you need a global property called "GL" for it to run, and the body needs to be loaded first, but that's all...it's called like: - LoadScript.include(scriptName)
don't call 'add' directly...other than that it works like an include/require script loader:) - LoadScript={
-
include:function(s){
-
if(!GL.sDiv){
-
GL.sDiv=document.createElement('div');
-
document.body.appendChild(GL.sDiv);
-
}
-
if(GL.sDiv[s]){GL.sDiv.removeChild(GL.sDiv[s])}
-
GL.sDiv[s]=LoadScript.add(s)
-
},
-
require:function(s){
-
if(!GL.sDiv){
-
GL.sDiv=document.createElement('div');
-
document.body.appendChild(GL.sDiv);
-
}
-
GL.sDiv[s]=GL.sDiv[s] || LoadScript.add(s);
-
},
-
add:function(s){
-
GL.sDiv[s]=document.createElement('script');
-
GL.sDiv[s].type='text/javascript';
-
GL.sDiv[s].src=s+'.js';
-
GL.sDiv.appendChild(GL.sDiv[s])
-
}
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
by: John Bowling |
last post by:
I'm creating a routine (not in a browser) to move multiple files on a daily
basis to a backup directory. It can be done easily by call shell functions
like 'mv file* newdir'.
I can't find any...
|
by: Julia Briggs |
last post by:
Hello World - I admit I'm new to javascript but I've tried for days to
find a solution to my problem.
Basically I have 3 unique javascript files that do different screen
display events that I...
|
by: Charlie |
last post by:
Ok, I have three files: calc.cpp, calc.h, and ui.cpp.
I would like to call a function that is located in ui.cpp from main()
in calc.cpp.
Both files have calc.h included, but when I tried to...
|
by: Deniz Bahar |
last post by:
Hi,
I would like to call one of my functions the exact name as an existing
C library function (for example K&R2 exercises asks me to make an atof
function). If I don't include the header with...
|
by: Ross A. Finlayson |
last post by:
Hi,
I hope you can help me understand the varargs facility.
Say I am programming in ISO C including stdarg.h and I declare a
function as so:
void log_printf(const char* logfilename, const...
|
by: Bill Grigg |
last post by:
I have been successfully calling DLL's using VC++ 6.0 and also using VC++7.1
(.NET). I only mention this because I have never felt comfortable with the
process, but nonetheless it did work....
|
by: Ron |
last post by:
Greetings,
I am trying to understand the rational for Raising Events
instead of just calling a sub. Could someone explain the
difference between the following 2 scenarios? Why would I
want to...
|
by: Stinky Pete |
last post by:
Hi (again) ;-)
I'm still very much at the bottom of a steep learning curve with VB, so
any and all help is always appreciated.
I've found some code to generate the user names who have logged...
|
by: nitusa |
last post by:
Hey Everyone,
I am doing a VB6 to C# conversion and everything was going smoothly until I realized that I needed to call a Fortran 77 (.for) .dll inside my code. I have looked through everything...
|
by: Stewart Berman |
last post by:
I am trying to develop a wrapper class for the Windows API functions in Visual Studio 2008:
GetOpenFileName
GetSaveFileName
I put together a starter class:
using System;
using...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |