473,231 Members | 1,581 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,231 software developers and data experts.

Including javascript within a js file

I'm looking for a way to include javascript files from within a ".js"
file. This would allow me to only need to link to one ".js" file, and
yet still organize my functions into non gargantuan files for easy
editing. I'm hoping there is some sort of include or import directive
that I could use. Or if no such directive exists, I'm wondering if
anyone has written one which I could use.

I need to do this without any server side scripting. For now at least,
the html is being used locally with local files.

I've found this:
http://www.forum4designers.com/archi...4-1-28286.html post about
the same problem, and was hoping that either someone here would have
more light to shed on the issue, or that there might be a way to do
this in the special situation of just working on a local, writable
drive.

-Trenqo

Jul 23 '05 #1
9 6093
ASM
Trenqo 0 wrote:
I'm looking for a way to include javascript files from within a ".js"
file. This would allow me to only need to link to one ".js" file, and
yet still organize my functions into non gargantuan files for easy
editing.


<script type="text/javascript">
// <![CDATA[

function includejs(file) {
document.write('<script src="' + file +
'"type="text/javascript"><\/script>');
}

function one() { blah }
function two() { bloh }
includejs('foo.js');
function three() { bluh }

// ]]>
</script>
--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #2
ASM wrote:
<snip>
<script type="text/javascript">
// <![CDATA[
In an HTML document the content type of a script element is CDATA. The
javascript comment makes this a redundant practice in an HTML document,
doubly redundant because the content is CDATA to start with.
function includejs(file) {
document.write('<script src="' + file +
'"type="text/javascript"><\/script>');
}


To date XHTML DOMs have tended not to support the - document.write -
method. Finding a call to the - document.write - method inside a
<![CDATA[ ... ]]> wrapper suggests misconceptions about the mark-up
being used and its relationship to the DOM being scripted.

Richard.
Jul 23 '05 #3
Trenqo 0 wrote:
I'm looking for a way to include javascript files from within a ".js"
file. This would allow me to only need to link to one ".js" file, and
yet still organize my functions into non gargantuan files for easy
editing. I'm hoping there is some sort of include or import directive
that I could use. Or if no such directive exists, I'm wondering if
anyone has written one which I could use.
Use the following:

Include the following tag the HTML file(s) just after the body tag:

<script type="text/javascript" src="include.js"></script>
The file 'include.js' contains:

var scripts = [
'script_1.js',
'script_2.js'
];
if ( document.getElementsByTagName ) {
var head = document.getElementsByTagName('HEAD')[0]
var scriptElement;
var i = scripts.length;
while ( i-- ) {
scriptElement = document.createElement('script');
scriptElement.type = 'text/javascript';
scriptElement.src = scripts[i];
head.appendChild(scriptElement);
}
}
I seem to be able to get away with putting the script above script
element in the head, but I'm not sure that will be OK for all browsers.
It should be safe as the first element of the body.

I need to do this without any server side scripting. For now at least,
the html is being used locally with local files.


Provided the links point to the files, life will be fine. You may need
to check the order in which files are loaded, you don't want to be
calling functions that aren't loaded yet.

[...]

--
Rob
Jul 23 '05 #4
VK
> I'm looking for a way to include javascript files
from within a ".js" file.


Well, the trully orthodox way is by using "import" instruction inside
of your code.

The catch 22 is that both main script and the "source" script have to
be signed by the same principals (So say if the main script signed by
John Doe, you can import only from archives signed by John Doe).

The catch 23 is that FF needs signed .jar file with JavaScript in it.
And IE needs signed .cab with JScript

All of above makes the import technologies nearly impossible. Over the
7 years this technologies exists I saw just a few such solutions, and
all of them for inside use (where the compatibility is not such an
issue).

So I would stay with RobG. Still good to know other options, I guess :-)

Jul 23 '05 #5
ASM
Richard Cornford wrote:
ASM wrote:
<snip>
<script type="text/javascript">
// <![CDATA[

In an HTML document the content type of a script element is CDATA. The
javascript comment makes this a redundant practice in an HTML document,
doubly redundant because the content is CDATA to start with.


My english being quite poor ... do you mean
<![CDATA[ ... ]]>
is not to use ?
(it is what I understand)

If that's right, why Tidy (from W3C) use that ?
function includejs(file) {
document.write('<script src="' + file +
'"type="text/javascript"><\/script>');
}

To date XHTML DOMs have tended not to support the - document.write -
method.


Most of time I do javascript for NC4.5
Sometimes I add few DOM code when this code is only cosmetic
and of no importance in general fonctionalities of the html page.
Finding a call to the - document.write - method inside a
<![CDATA[ ... ]]> wrapper suggests misconceptions about the mark-up
being used and its relationship to the DOM being scripted.
So, how have I to write correctly (with a transitional doctype)
<script type= ....
and
this document.write() ?
Richard.


--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #6
ASM
RobG wrote:

Use the following:

Include the following tag the HTML file(s) just after the body tag:

<script type="text/javascript" src="include.js"></script>
The file 'include.js' contains:

var scripts = [
'script_1.js',
'script_2.js'
];
if ( document.getElementsByTagName ) {
Oh yes ! instersting method ! and for my NC 4.5 ?

var head = document.getElementsByTagName('HEAD')[0]
var scriptElement;
var i = scripts.length;
while ( i-- ) {
scriptElement = document.createElement('script');
scriptElement.type = 'text/javascript';
scriptElement.src = scripts[i];
head.appendChild(scriptElement);
}
}


--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #7
ASM wrote:
[...]

Oh yes ! instersting method ! and for my NC 4.5 ?


The scripts will not be added as Netscape Navigator 4 (NN4) does not
support getElementsByTagName.

If all JS functionality is added using the suggested method, NN4 users
will get the default site without JavaScript - which may be a distinct
advantage, given that the use of NN4 indicates a very old PC that may
perform better without unnecessary scripts slowing it down.
--
Rob
Jul 23 '05 #8
ASM
RobG wrote:
ASM wrote:
[...]

Oh yes ! instersting method ! and for my NC 4.5 ?

The scripts will not be added as Netscape Navigator 4 (NN4) does not
support getElementsByTagName.


it was the why of my question
If all JS functionality is added using the suggested method, NN4 users
will get the default site without JavaScript - which may be a distinct
advantage, given that the use of NN4 indicates a very old PC that may
perform better without unnecessary scripts slowing it down.


we'll say that :-/

I keep your script as a good example of objects creation and placement

--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #9
ASM schrieb:
Richard Cornford wrote:
ASM wrote:
<script type="text/javascript">
// <![CDATA[
In an HTML document the content type of a script element is CDATA.
The javascript comment makes this a redundant practice in an HTML
document, doubly redundant because the content is CDATA to start
with.


My english being quite poor ... do you mean
<![CDATA[ ... ]]>
is not to use ?
(it is what I understand)


It is not to be used in HTML documents served as text/html and JS
script files, and it is not necessary to be commented out in XHTML
documents served as application/xhtml+xml; serving XHTML as
text/html is considered harmful.
If that's right, why Tidy (from W3C) use that ?


Because it is a little bit buggy. Note: HTML Tidy is not from W3C,
it was originally developed by a W3C member and is now maintained by
several developers, obtainable from SourceForge.
function includejs(file) {
document.write('<script src="' + file +
'"type="text/javascript"><\/script>');
}

To date XHTML DOMs have tended not to support the - document.write
- method.


Most of time I do javascript for NC4.5
Sometimes I add few DOM code when this code is only cosmetic
and of no importance in general fonctionalities of the html page.


But in this case it is, you just didn't recognize it.
Finding a call to the - document.write - method inside a
<![CDATA[ ... ]]> wrapper suggests misconceptions about the mark-up
being used and its relationship to the DOM being scripted.


So, how have I to write correctly (with a transitional doctype)
<script type= ....
and
this document.write() ?


Using node-accessing and node-creating methods introduced in W3C DOM
Level 1 HTML and DOM Level 2 Core; using the proprietary variants if
you need to.
PointedEars
Jul 23 '05 #10

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

Similar topics

1
by: Catherine Lynn Smith | last post by:
I am trying to figure out the recommended way to include javascript 'from' javascript. I found some references that say to just write a 'script' tag to the document, but I am curious about the...
4
by: Ralf Koms | last post by:
Hi, I would like to reference some other HTML files within an "main" HTML file (within the "header"), Something like this: <link rel="part1" href="file1.htm"> <link rel="part2"...
3
by: Joseph Ferris | last post by:
Hello everyone. I recently reloaded my system, and I am now having problems getting one of my client's sites running. I pulled a copy of the development tree for the project out of subVersion...
8
by: david.lindsay.green | last post by:
Hello all, I am quite new a web scripting and making web pages in general and I have stumbled across a problem I have as yet been unable to solve. I am trying to take the contents of a textarea box...
5
by: Chris Ashley | last post by:
Hi there, I need to include an ASPX page within an ASP page. Is this possible? EG: making-a-claim.asp is: <!-- #include file="top.asp" -->
3
by: Mr. Roper | last post by:
I'm pretty weak when it comes to Java script, hopefully someone will take mercy on my sole and explain this to me. How come on the following HTML page, when I have my first script tag commented...
3
by: u0107 | last post by:
Hello, I have a multi-frame page. The frames are named Frame_1, Frame_2 and Frame_3. Frame_1 has a drop down box. When a value is selected in this drop down box, Frame_2 is updated using an...
38
by: Neo Geshel | last post by:
I am seeking a method to load one JS file directly into another, *without* having to dynamically write <scripttags. Is there any method whereby I can call only one external JS file using a ...
1
by: vunet | last post by:
What's the possible disadvantage of including JavaScript files within a JavaScript file? Why I needed it? For the sake of easy management, perhaps? Any load speed disadvantage? Example below: ...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.