Including one external JS file directly into another

P

pcx99

That's a really nice mod! :) Clipped for the toolbox.

Thanks!


-Lost said:
I am seeking a method to load one JS file directly into another, *without* having to
dynamically write <script> tags.
Is there any method whereby I can call only one external JS file using a single <script>
tag, but have that external JS file insert into ITSELF the contents of five others?

Based on Hunlock's lovely little snippet:

http://www.hunlock.com/blogs/Howto_Dynamically_Insert_Javascript_And_CSS

I wrote:

function embed_scripts()
{
var arg_obj = String(arguments[0]);
var arg = (arguments.length == 1) ? arg_obj.split(',') : arguments;
var head_obj = document.getElementsByTagName('head')[0];
for (var i = 0; i < arg.length; i++)
{
var script_obj = document.createElement('script');
script_obj.type = 'text/javascript';
script_obj.src = arg + '.js';
head_obj.appendChild(script_obj);
}
}

It can accept either an array:

var _scripts = ['script1', 'script2'];
window.onload = embed_scripts(_scripts);

...or a comma-separated list of arguments:

window.onload = embed_script('script1', 'script2');

Now, just because it worked flawlessly for me does not mean it is bulletproof. I may not
have tested all situations or it could be coincidental that it works.

One thing for certain, it makes no attempt to check whether or not 'script1' is a valid
file.

Let's see who "slams my head against the wall." *grin*

-Lost
 
D

dd

I am seeking a method to load one JS file directly into another,
*without* having to dynamically write <script> tags.
Is there any method whereby I can call only one external JS file using a
single <script> tag, but have that external JS file insert into ITSELF
the contents of five others?
Something like:

/*start external.js*/
import ("/js/script1.js")
import ("/js/script2.js")
import ("/js/script3.js")
import ("/js/script4.js")
import ("/js/script5.js")
/*end external.js*/

The only way to do this immediately without giving IE a heart
failure (i.e. not wait for onload event) is to use script tags with
document.write:

document.write ("<scr"+"ipt language='javascript' src='/js/
script1.js'></scr"+"ipt>")
document.write ("<scr"+"ipt language='javascript' src='/js/
script2.js'></scr"+"ipt>")
document.write ("<scr"+"ipt language='javascript' src='/js/
script3.js'></scr"+"ipt>")
document.write ("<scr"+"ipt language='javascript' src='/js/
script4.js'></scr"+"ipt>")
document.write ("<scr"+"ipt language='javascript' src='/js/
script5.js'></scr"+"ipt>")

Before you flame me and say this isn't what you want,
listen to this ::: --->> *** IT IS WHAT YOU WANT ***.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
ospam.invalid>, Sat, 3 Feb 2007 19:45:40, John G Harris
PS Your sig has Unicode characters that don't display properly in ASCII
readers.

Not important. It is, however, improperly delimited and, like it's
poster's ego, grossly inflated and unjustified. Ostracism is the
answer.
 
J

John G Harris

I need to be able to load different files depending on which features
exist in the calling page.
<snip>

You want conditional includes ? That's not what
/*start external.js*/
import ("/js/script1.js")
import ("/js/script2.js")
import ("/js/script3.js")
import ("/js/script4.js")
import ("/js/script5.js")
/*end external.js*/
suggests.




There are still ASCII Usenet readers out there that can't understand
Unicode?? Wow.
<snip>

My font setting is one that excludes Unicode characters. That's my
choice, not something imposed on me by Turnpike. Oh, and phishing
e-mails look a total mess and I don't care at all.

John
 
D

Dr J R Stockton

In comp.lang.javascript message said:
Okay, so I got a little irritated. But I specifically stated what I
*did not* want to see, and yet that’s all I got in response.

No, your natural vulgarity was fully exposed.

As for the sig, you’re right. It’s an old one for this computer, and I
haven’t updated it with my current one in a while. Done.


It's still not compliant with accepted norms.

Moreover, its second text line threatens retaliation for what may be a
pure accident. KETFOB.
 
N

Neo Geshel

Dr said:
It's still not compliant with accepted norms.

Moreover, its second text line threatens retaliation for what may be a
pure accident. KETFOB.

It’s an *automatically monitored* spam honeypot. One of about twodozen
e-mails that I have my server monitoring. I never access it. Anything
that makes its way into that account is automatically processed by the
server and reported to SpamCop. That’s why I provided the cautionary
warning in the first place.

At least I made it known *why* people shouldn’t send e-mail to my
reply-to, rather than letting them find out when their service provider
cuts them off. I have known several service providers to be rather
twitchy, and have disconnected people for having sent out just a few
unsolicited e-mails.

And BTW, what are “accepted norms� Please, do tell.

Cheers.
...Geshel
--
***********************************************************************
My return e-mail address is an automatically monitored spam honeypot.
Do not send e-mail there unless you wish to be reported as a spammer.
Please send all e-mail to my first name at my last name dot org, with
a subject-line of “NEWSGROUP REPLY FOR NEO GESHEL†(alluppercase).
***********************************************************************
 
L

Lee

Neo Geshel said:
And BTW, what are =E2=80=9Caccepted norms=E2=80=9D? Please, do tell.

A signature at the end of a posting should be no more than 80
columns wide and four lines long. It is signified by two dashes
and a space on a line of their own, which causes everything
afterward, including stray quoted material, to be counted as
part of the signature. In the signature, one may choose to place
their name and contact information, or perhaps a quote or a witty
saying. One is advised to exercise in moderation in picking their
signature. Signatures are not expected to be quoted in followups,
and many newsreaders will strip them automatically.
o Religious beliefs, political stances, or other strong opinions
are inappropriate for inclusion when posting in groups where
that is not a topic, even as signature.


--
 
N

Neo Geshel

Lee said:
Neo Geshel said:


A signature at the end of a posting should be no more than 80
columns wide and four lines long. It is signified by two dashes
and a space on a line of their own, which causes everything
afterward, including stray quoted material, to be counted as
part of the signature. In the signature, one may choose to place
their name and contact information, or perhaps a quote or a witty
saying. One is advised to exercise in moderation in picking their
signature. Signatures are not expected to be quoted in followups,
and many newsreaders will strip them automatically.
o Religious beliefs, political stances, or other strong opinions
are inappropriate for inclusion when posting in groups where
that is not a topic, even as signature.
Well, judging from this explanation, I seem to be following the
“accepted norms†quite closely with my newly modified sig.

It has the double-dash to start it off, and is four lines of text that
are each less than 72 characters long (Thunderbird wraps at 72
characters by default).

Granted, the text is buttressed by two lines of astrixes, but that’s
just to emphasize the text. I just thought that this would be more
acceptable as a highlighting method THAN A SIG TOTALLY IN UPPERCASE.

Or is an ALL UPPERCASE SIG MORE ACCEPTABLE than all astrixes? I seem to
be rather offensive as of late, so please advise which of the two would
be more appropriate.

...Geshel
--
***********************************************************************
My return e-mail address is an automatically monitored spam honeypot.
Do not send e-mail there unless you wish to be reported as a spammer.
Please send all e-mail to my first name at my last name dot org, with
a subject-line of “NEWSGROUP REPLY FOR NEO GESHEL†(alluppercase).
***********************************************************************
 
F

find clausen

document.write ("<scr"+"ipt language='javascript' src='/js/
script2.js'></scr"+"ipt>")

But this will only work online.
And what if the documents are in different sub folders:
.../
.../../
 
R

Randy Webb

find clausen said the following on 2/11/2007 5:53 AM:
But this will only work online.

It will work offline as well if the file is local. But, the way that
document.write statement is written is very subpar at best.
And what if the documents are in different sub folders:
../
../../

Then you change the path accordingly.
 
F

find clausen

Then you change the path accordingly.

hmm.

I use this:

The toolbox:

<script src="../noindx/scripts/set.js"></script>

then on some pages for language modules:

document.write('<script
src="../noindx/scripts/'+lang+'.js"><\/script>')

I want to attach the lang to the set.js

that works allmost ok in a "flat structure" : all pages in same level
from the root.

But not on pages one or more ../ away from the root, understand this ?
i.e

/romo/ : ok

/romo/folder/ : not ok
 
R

Randy Webb

find clausen said the following on 2/15/2007 1:18 PM:
hmm.

I use this:

The toolbox:

<script src="../noindx/scripts/set.js"></script>

then on some pages for language modules:

document.write('<script
src="../noindx/scripts/'+lang+'.js"><\/script>')

I want to attach the lang to the set.js

that works allmost ok in a "flat structure" : all pages in same level
from the root.

But not on pages one or more ../ away from the root, understand this ?
i.e

/romo/ : ok

/romo/folder/ : not ok

Then use absolute paths to the .js file, read that path and modify it
accordingly. Or, you could try traversing the sub-folders but using an
absolute path makes it simple.
 
F

find clausen

Then use absolute paths to the .js file, read that path and modify it
accordingly. Or, you could try traversing the sub-folders but using an
absolute path makes it simple.

But absolute ... does not work offline, when editing.
I could also make a copy of the script folder,
placing it in the right position/path :)

I thought there was a programming way to correct the path. (?)
 
R

Randy Webb

find clausen said the following on 2/16/2007 8:25 AM:
But absolute ... does not work offline, when editing.
I could also make a copy of the script folder,
placing it in the right position/path :)

I thought there was a programming way to correct the path. (?)

Use a <BASE HREF=""> and you can test with relative paths. The path will
be relative to the HREF given. If you are on-line, use an on-line HREF.
If you are off-line then use the local HREF.
 
L

-Lost

Randy Webb said:
find clausen said the following on 2/16/2007 8:25 AM:

There is, remember the file protocol.

file:///x:/path/to/js/file.js works just fine locally, and offline. Online, it will not.

Use a <BASE HREF=""> and you can test with relative paths. The path will be relative to
the HREF given. If you are on-line, use an on-line HREF. If you are off-line then use
the local HREF.

And of course this is also works. It might even be easier on you depending on how many
links are involved.

-Lost
 
C

cpmmug

Within the HTML of the Web page, try...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<link rel="stylesheet" type="text/css" href="/css/style.css"
media="screen,print" />
<script type="text/javascript" language="javascript" src="/javascripts/
master.js"></script>
</head>
<body>
</body>
</html>

Now, within the master.js file within your javascripts folder, try...

document.write('<script type="text/javascript" language="javascript"
src="/javascripts/your1stexternaljavascripthere.js"><\/script>');
document.write('<script type="text/javascript" language="javascript"
src="/javascripts/your2ndexternaljavascripthere.js"><\/script>');
document.write('<script type="text/javascript" language="javascript"
src="/javascripts/your3rdexternaljavascripthere.js"><\/script>');
document.write('<script type="text/javascript" language="javascript"
src="/javascripts/your4thexternaljavascripthere.js"><\/script>');
document.write('<script type="text/javascript" language="javascript"
src="/javascripts/your5thexternaljavascripthere.js"><\/script>');

I hope this helps. So far, it seems to work in Firefox 2.0.0.2 and
Internet Explorer 6.0.2900.2180.xpsp_sp2_rtm ...
 
O

OmegaJunior

Within the HTML of the Web page, try...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<link rel="stylesheet" type="text/css" href="/css/style.css"
media="screen,print" />
<script type="text/javascript" language="javascript" src="/javascripts/
master.js"></script>
</head>
<body>
</body>
</html>

Now, within the master.js file within your javascripts folder, try...

document.write('<script type="text/javascript" language="javascript"
src="/javascripts/your1stexternaljavascripthere.js"><\/script>');
document.write('<script type="text/javascript" language="javascript"
src="/javascripts/your2ndexternaljavascripthere.js"><\/script>');
document.write('<script type="text/javascript" language="javascript"
src="/javascripts/your3rdexternaljavascripthere.js"><\/script>');
document.write('<script type="text/javascript" language="javascript"
src="/javascripts/your4thexternaljavascripthere.js"><\/script>');
document.write('<script type="text/javascript" language="javascript"
src="/javascripts/your5thexternaljavascripthere.js"><\/script>');

I hope this helps. So far, it seems to work in Firefox 2.0.0.2 and
Internet Explorer 6.0.2900.2180.xpsp_sp2_rtm ...

--
Virtually,
CPMMUG Manager
(e-mail address removed)
http://www.cpmmug.com

Hm. I've run into problems with this approach.

It works when sending the page as text/html. But as soon as you upgrade to
xhtml 1.1 or xhtml 2 and try to send the page as application/xhtml, this
same approach will fail. So keep your page text/html and it'll keep
working.

Google Maps uses this same technique and is troubled with the same
problem. I described the mentioned problem a bit more exhaustively here:
http://www.sdsmedia.com/journal.php?do=showentry&e=1819&enum=42
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top