473,803 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

import/include a .js file into a .js file

Hi,

Is there a way to include a .js file inside a .js file in JavaScript 1.5?

Thanx

Henri

Jul 23 '05 #1
8 207674
In article <41************ **********@news .wanadoo.fr>, hm********@hotm ail.com
enlightened us with...
Hi,

Is there a way to include a .js file inside a .js file in JavaScript 1.5?


Not that I know of. If you figure one out, I'd love to see it. I'm tired of
having to copy/paste library functions, too.

--
--
~kaeli~
If a turtle doesn't have a shell, is he homeless or naked?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #2
kaeli wrote:
In article <41************ **********@news .wanadoo.fr>, hm********@hotm ail.com
enlightened us with...
Hi,

Is there a way to include a .js file inside a .js file in JavaScript 1.5?

Not that I know of. If you figure one out, I'd love to see it. I'm tired of
having to copy/paste library functions, too.


In the included file either document.write the script tag to include the
second file, or, createElement and load it.

Of course, with server side coding, it becomes trivial to add an include
statement to include the external file.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq
Jul 23 '05 #3
In article <GN************ ********@comcas t.com>, Hi************@ aol.com
enlightened us with...

In the included file either document.write the script tag to include the
second file, or, createElement and load it.

Of course, with server side coding, it becomes trivial to add an include
statement to include the external file.


I don't think that would work for what I want it for.

I have a library file called jsValidation.js with generic functions for
things like testing for blank fields, format of phone number, and so on.

I also have one called jsEvents.js with a way to add events in a cross-
browser fashion. It has a function called addEvent.

Now, say I want to make a third js file and I want to call functions isBlank
from the validation file and addEvent from the event file.

I don't think I can do that. Do you know a way?

--
--
~kaeli~
If God dropped acid, would he see people?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #4
Unless I'm misunderstandin g this, you're barking up the wrong tree here.
You don't call js functions from files, you load the files into RAM (the
js memory space) and then simply simply call any functions needed; if in
scope, they'll run. The only difference between embedded (hardcoded) js
and 'external' js is that one is - erm - embedded in the main document
and read into memory from there, and the other is stored in a separate
file and loaded from there. No other distinctions: 'external' js doesn't
sort of "hover out there" as many seem to thing, waiting to be "called".
It's either loaded or it's not, available or not.

There are apis with member functions specifically designed to 'import'
needed external files (using document.write to print script tags) but
they're highly proprietary.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #5
kaeli...

btw love your sig..Totally 60s. :D

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #6
In article <41************ **********@news .newsgroups.ws> , ferndoc9
@hotmail.com enlightened us with...
Unless I'm misunderstandin g this, you're barking up the wrong tree here.


Not quite. But I did make a bit of a logic error. I'm too used to compiled
code. ;)
I want to ensure both files are loaded into memory when only one is
specifically included in the client html file with <script src=> type tag.

Since I only use script in my intranet apps, and we only support DOM
browsers, I see now that I can simply add the other files like so:

/* jsHandler.js */
function inc(filename)
{
var body = document.getEle mentsByTagName( 'body').item(0) ;
script = document.create Element('script ');
script.src = filename;
script.type = 'text/javascript';
body.appendChil d(script)
}

inc("jsValidati on.js");
inc("jsEvents.j s");

/* several more functions that call functions in the above two files */
function blah (foo)
{
if (isBlank(foo)) return false;
}

/* end */

Then in my JSP, I can just do
<script type="text/javascript" src="jsHandler. js"></script>
and it will get both the other files as well.

I suspect there would be problems with scripts that try to run prior to page
load (such as image preloaders), but none of mine do that. My stuff is pretty
much all form validation and dynamically adding event handlers to JSP-
generated form elements.

Thanks for the tips.

--
--
~kaeli~
Profanity: the single language in which all programmers are
expert.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #7
This is the best solution I've found so far:

At the beginning of each .js file importing another file:
(this file defines a class called 'myclass' and 'km_' is just the prefix I
use for my functions or classes)

----------------------------------------------------------------
// myclass.js

if (typeof(km_scri pts) == 'undefined') var km_scripts = new Object();
km_myclass_impo rt('importedfil e.js');

function km_myclass_impo rt(jsFile) {
if (km_scripts[jsFile] != null) return;
var scriptElt = document.create Element('script ');
scriptElt.type = 'text/javascript';
scriptElt.src = jsFile;
document.getEle mentsByTagName( 'head')[0].appendChild(sc riptElt);
km_scripts[jsFile] = jsFile; // or whatever value your prefer
}

function km_myclass_aler t() {
alert(importedV alue);
}

-------------------------------------------------------------
The imported file importedfile.js :

// importedfile.js

var importedValue = 'The file was imported';

----------------------------------------------------------------
The HTML file to test it :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<script language="javas cript" src="myclass.js " />
<TITLE></TITLE>
</HEAD>
<BODY>
<input type="button" value="Test!" onclick="km_myc lass_alert()" />
</BODY>
</HTML>
Thanx for your helps
Henri
"kaeli" <ti******@NOSPA M.comcast.net> a écrit dans le message de
news:MP******** *************** *@nntp.lucent.c om...
In article <41************ **********@news .wanadoo.fr>, hm********@hotm ail.com enlightened us with...
Hi,

Is there a way to include a .js file inside a .js file in JavaScript 1.5?
Not that I know of. If you figure one out, I'd love to see it. I'm tired

of having to copy/paste library functions, too.

--
--
~kaeli~
If a turtle doesn't have a shell, is he homeless or naked?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace


Jul 23 '05 #8
kaeli wrote:
In article <GN************ ********@comcas t.com>, Hi************@ aol.com
enlightened us with...
In the included file either document.write the script tag to include the
second file, or, createElement and load it.

Of course, with server side coding, it becomes trivial to add an include
statement to include the external file.

I don't think that would work for what I want it for.

I have a library file called jsValidation.js with generic functions for
things like testing for blank fields, format of phone number, and so on.

I also have one called jsEvents.js with a way to add events in a cross-
browser fashion. It has a function called addEvent.

Now, say I want to make a third js file and I want to call functions isBlank
from the validation file and addEvent from the event file.

I don't think I can do that. Do you know a way?


<script src="myCustomJS File.php" type="text/javascript"></script>

And in myCustomJSFile. php:

include 'jsValidation.j s';
include 'jsEvents.js';
include 'thirdJSFile.js ';

//rest of unique to the page JS code here
When the browser gets it, all in one file, are all the functions/code
you want.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2727
by: Bill Davy | last post by:
I am working with MSVC6 on Windows XP. I have created an MSVC project called SHIP I have a file SHIP.i with "%module SHIP" as the first line (file is below). I run SHIP.i through SWIG 1.3.24 to obtain SHIP_wrap.cpp and SHIP.py; the latter contains the line "import _SHIP". I compile SHIP_wrap.cpp and a bunch of files into a DLL which I have the
3
13505
by: ch424 | last post by:
Hi there, I'm using Python 2.4.1 on Ubuntu Linux, and I'm having problems extending python in C: The C code is below: #include <Python.h> #include "ni488.h"
1
3072
by: mirandacascade | last post by:
O/S: Windows 2K Vsn of Python: 2.4 Currently: 1) Folder structure: \workarea\ <- ElementTree files reside here \xml\ \dom\
2
3229
by: Keith Chadwick | last post by:
I have been running some tests with regards to xsl:include and xsl:import with the same results on both and I am wondering if someone can explain this behavior to me! First off the xslt file is being loaded as in dim myXSLT as new XslTransform() myXSLT.load("scripts/clnt.home.body.xslt") The load craps out when I change the xsl:include, tried all of the following
1
4337
by: Stephen Edgecombe | last post by:
Hi Environment: Visual Studio .NET 2003 (vb) I am developing an XSD Schema and want to include some building blocks from another source, so they have a different namespace. - If I use an <Include> the Schema doesn't show any of the Elements from the include Schema. But that's to be expected because they are in a different namespace. - If I use <Import> the Schema doesn't show any of the Elements from the
2
3378
by: s.hong | last post by:
Hello? On the ASP.NET, I can use the class by import declation on the top of page. <%@ page language="vb" explicit="true" aspcompat="false" debug="true"%> <%@ import namespace="System.Data" %> <%@ import namespace="System.Data.SqlClient" %> It's work well for the compiled class files in /bin folder.
4
6114
by: Bruce W. Roeser | last post by:
All, I'm reading a book by Charles Petzold (Programming VS.Net). Pretty good content but am confused about the difference. From the text: ---------------------------------------------------------------------------------------------------------------------------------------------------------- The @ Import Directive Next to @ Page, the directive that ASP.NET programmers use the most is @ Import. The @ Import directive is ASP.NET's...
6
2366
by: robert | last post by:
I get python crashes and (in better cases) strange Python exceptions when (in most cases) importing and using cookielib lazy on demand in a thread. It is mainly with cookielib, but remember the problem also with other imports (e.g. urllib2 etc.). And again very often in all these cases where I get weired Python exceptions, the problem is around re-functions - usually during re.compile calls during import (see some of the exceptions below). But...
49
3951
by: Martin Unsal | last post by:
I'm using Python for what is becoming a sizeable project and I'm already running into problems organizing code and importing packages. I feel like the Python package system, in particular the isomorphism between filesystem and namespace, doesn't seem very well suited for big projects. However, I might not really understand the Pythonic way. I'm not sure if I have a specific question here, just a general plea for advice. 1) Namespace....
2
5815
by: worlman385 | last post by:
So the difference between import and include is that: import = used for binary library like DLL or .Lib files. it's very similar to include that it load all the header (function definition) from the DLL file so that we can use the header file just like include include = just include header (source file) ==========================
0
9699
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10542
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10309
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10289
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6840
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5496
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4274
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2968
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.