473,382 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 207638
In article <41**********************@news.wanadoo.fr>, hm********@hotmail.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********@hotmail.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.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #3
In article <GN********************@comcast.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 misunderstanding 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 misunderstanding 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.getElementsByTagName('body').item(0);
script = document.createElement('script');
script.src = filename;
script.type = 'text/javascript';
body.appendChild(script)
}

inc("jsValidation.js");
inc("jsEvents.js");

/* 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_scripts) == 'undefined') var km_scripts = new Object();
km_myclass_import('importedfile.js');

function km_myclass_import(jsFile) {
if (km_scripts[jsFile] != null) return;
var scriptElt = document.createElement('script');
scriptElt.type = 'text/javascript';
scriptElt.src = jsFile;
document.getElementsByTagName('head')[0].appendChild(scriptElt);
km_scripts[jsFile] = jsFile; // or whatever value your prefer
}

function km_myclass_alert() {
alert(importedValue);
}

-------------------------------------------------------------
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="javascript" src="myclass.js" />
<TITLE></TITLE>
</HEAD>
<BODY>
<input type="button" value="Test!" onclick="km_myclass_alert()" />
</BODY>
</HTML>
Thanx for your helps
Henri
"kaeli" <ti******@NOSPAM.comcast.net> a écrit dans le message de
news:MP************************@nntp.lucent.com...
In article <41**********************@news.wanadoo.fr>, hm********@hotmail.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********************@comcast.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="myCustomJSFile.php" type="text/javascript"></script>

And in myCustomJSFile.php:

include 'jsValidation.js';
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.javascript 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
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...
3
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
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
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...
1
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...
2
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" %>...
4
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: ...
6
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...
49
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...
2
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.