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

Attempting nested javascript include

Hello,
I have been trying to include a js file from within another js file. I
can get this to work for firefox, but not i.e.

What I have is
a.html ...
<script language='JavaScript'
src='b.js'></script><script>Function();</script>
b.js ....
function Function() {
var s1 = "";
document.write("<sc" + s1+ "ript language='JavaScript'
src='c.js'></sc" + s1 + "ript>");
document.write("<sc" + s1+ "ript language='JavaScript'>Func2();</sc" +

s1 + "ript>");
}
and then c.js
I tried the s1="" from another googled post - no joy.
if I change a.html to
<script language='JavaScript' src='cb.js'></script>
<script language='JavaScript'
src='b.js'></script><script>Function();</script>
it works.

Is anyone able to get this to work with IE 6?

Thanks - Bryan.

Jul 23 '05 #1
3 11806

"Brit" <mo***@english.net> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hello,
I have been trying to include a js file from within another js file. I
can get this to work for firefox, but not i.e.

What I have is
a.html ...
<script language='JavaScript'
src='b.js'></script><script>Function();</script>
b.js ....
function Function() {
var s1 = "";
document.write("<sc" + s1+ "ript language='JavaScript'
src='c.js'></sc" + s1 + "ript>");
document.write("<sc" + s1+ "ript language='JavaScript'>Func2();</sc" +

s1 + "ript>");
}
and then c.js
I tried the s1="" from another googled post - no joy.
if I change a.html to
<script language='JavaScript' src='cb.js'></script>
<script language='JavaScript'
src='b.js'></script><script>Function();</script>
it works.

Is anyone able to get this to work with IE 6?

Thanks - Bryan.


Here's what I use (I have this in a 'util.js' file that I load 'manually'
(with a <script src=...> element):
====================
var Included={};
function Include() {
var src, url, heads;
heads = document.getElementsByTagName("head");
if (heads.length==0) {throw new Error(0x8009000,"Include: No <head> tag
exists. Scripts not loaded.");}
url = document.URLUnencoded;
for (a=0; a<arguments.length; a++) {
src = arguments[a];
if (Included[src]) continue;
Included[src] = true;
if (url.substr(0,5)=="file:") {
if (src.charAt(0)=="/") src = "C:<your webroot directory full path>"+src;
else src = url.slice(7,url.lastIndexOf("\\")+1)+src;
}
heads[0].appendChild(document.createElement("<script language='JavaScript'
src='"+src+"' />"));
}
}
====================
Then in the '<head>' I put:

<script
language="javascript">Include("/_ScriptLibrary/MyScript.js","LocalScripts.js",...)</script>

You may also use Include() in other 'Included' or manually loaded script
files, but may not reference their contents until the parent file is fully
loaded.

This works fine in IE6 but is untested in any other enviromant.
Jul 23 '05 #2
"Brit" <mo***@english.net> writes:
I have been trying to include a js file from within another js file. I
can get this to work for firefox, but not i.e.
*how* does it not work in IE? Does it give an error message? Do you
have Javascript error messages enabled?
<script language='JavaScript'
src='b.js'></script>
Should be
<script type="text/javascript" src="b.js"></script>
The "type" attribute is mandatory, and the language attribute is
deprecated and not needed.
<script>Function();</script>
....
function Function() {
*Bad* name for a function. There is already a built-in function called
Function.
var s1 = "";
document.write("<sc" + s1+ "ript language='JavaScript'
src='c.js'></sc" + s1 + "ript>");
No need for being this convoluted. Just write:
document.write("<script type='text/javascript' src='c.js'></script>");

Since this is inside a javascript file, and not inside an HTML file,
there is no need to escape anything.

If this had been inside a script tag in an HTML file, all you need to do
is change all "</" to "<\/", i.e.,
document.write("<script type='text/javascript' src='c.js'><\/script>");

Your attempt at escaping by putting "s1" in the middle would still
fail in a fully standards compliant HTML parser, because such one
would end the script tag at the first occurence of "</". Actual
browsers only end it at "</script", but there is no need to take
chances when being correct is so easy.
document.write("<sc" + s1+ "ript language='JavaScript'>Func2();</sc" +

s1 + "ript>");
}
and then c.js
.... defining Func2, I guess.
I tried the s1="" from another googled post - no joy.
And no need.
if I change a.html to
<script language='JavaScript' src='cb.js'></script>
<script language='JavaScript'
src='b.js'></script><script>Function();</script>
it works.

Is anyone able to get this to work with IE 6?


Again, what error message does it give you?

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #3
Thanks for the response. I stripped down the code and renamed the
functions - supposedly for clarity.

Anyway, I tried with the added type field, however that didn't make any
difference here. The error I get is 'Object Expected' with a code of 0.
The line it points to is the call to the function in b.js. If I remove
the call to Func2 in b.js, it works fine. If I replace the call to
Func2 in b.js, but include c.js in a.html it works. This is what I've
done for now - but it's annoying that it doesn't work with just
including b.js and calling Function.

Jul 23 '05 #4

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

Similar topics

3
by: Rubén Campos | last post by:
Organizing classes, types, structures, enums and whatever other entities into nested namespaces requires to include into every header and implementation file the complete path of namespaces. Let me...
7
by: Alfonso Morra | last post by:
I have a class that contains a nested class. The outer class is called outer, and the nested class is called inner. When I try to compile the following code, I get a number of errors. It is not...
2
by: Alfonso Morra | last post by:
I have a class that contains a nested class. The outer class is called outer, and the nested class is called inner. When I try to compile the following code, I get a number of errors. It is not...
11
by: charlie | last post by:
I recently came across s function (actually many) whose structure is a series of nested ifs with the meat of the function at the very centre: if (condition1) if (condition2) if (condition3) if...
2
by: Dustin | last post by:
I have a nested quotes issue I'm trying to resolve. Basically I need a 5th level nested quote for quoting the string argument to the getXMLFeed() function in the following: var myHTML = "<TR...
18
by: desktop | last post by:
I have 3 types of objects: bob1, bob2 and bob3. Each object is identified by a unique ID which gets returned by the function getId(). All bobs are descendants from class BaseBob which is an...
13
by: JRough | last post by:
I got lost on formatting the nested if/else's on the bottom of the file right up the last else/if there seems to be a stray bracket. I'm trying to line up the brackets so I can read it. Is it...
0
by: Tommy Jakobsen | last post by:
Hi. Is there a problem when trying to use Javascript on a nested masterpage? On my masterpage, i've got this in the <head>: <asp:ContentPlaceHolder ID="HeadContent" runat="server" /> And on...
2
card
by: card | last post by:
Hi everyone, I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes: #include...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.