Storing Scripts on a separate page

D

DeadCert

Hi, How can I store all my scripts on a separate page and then call
them from the html page that I want to use them on? the <head> section
is getting too cluttered and I would like to store all in one place.
Anyone know how? Cheers
 
J

Janwillem Borleffs

DeadCert said:
Hi, How can I store all my scripts on a separate page and then call
them from the html page that I want to use them on? the <head> section
is getting too cluttered and I would like to store all in one place.
Anyone know how? Cheers

<script type="text/javascript" src="some.js"></script>

Where some.js contains JS code only.


JW
 
B

Bas Cost Budde

Janwillem said:
<script type="text/javascript" src="some.js"></script>

Where some.js contains JS code only.

If the OP has several files of javascript already, that doesn't compress
to one statement, right? I tried this once--I found nothing to include
javascript from another javascript file, I seem to need a SCRIPT tag for
every file. Correct me if I'm wrong. I love to be wrong on such issues ;-)
 
B

Brian Genisio

Bas said:
If the OP has several files of javascript already, that doesn't compress
to one statement, right? I tried this once--I found nothing to include
javascript from another javascript file, I seem to need a SCRIPT tag for
every file. Correct me if I'm wrong. I love to be wrong on such issues ;-)

You are correct, but there is one solution that comes to mind (I am sure
there are others)...

myHtml.html:
<script type="text/javascript" src="some.js"></script>

some.js:
function writeScript( JSfile )
{
document.write("<script type='text/javascript' src='" +
JSfile + "'></scr" + "ipt>");
}

writeScript("file1.js");
writeScript("file2.js");
writeScript("file3.js");
....
 
J

Janwillem Borleffs

Bas Cost Budde said:
If the OP has several files of javascript already, that doesn't compress
to one statement, right? I tried this once--I found nothing to include
javascript from another javascript file, I seem to need a SCRIPT tag for
every file. Correct me if I'm wrong. I love to be wrong on such issues ;-)

You can include one js file, which includes the required files by document
writing the appropriate script tags.

Anyways, the OP would benefit from a thorough re-organisation of his
files...


JW
 
F

F. Da Costa

DeadCert said:
Hi, How can I store all my scripts on a separate page and then call
them from the html page that I want to use them on? the <head> section
is getting too cluttered and I would like to store all in one place.
Anyone know how? Cheers
I'm doing exactly the same using frames.
One of them is reserved for js only.

It has an additional (could be significant) benefit and that is that you
can also 'store' interpage states here. So loading a page with knowledge
about its previous state/ predecessor becomes possible as well.
Try and do this with a js variable in the page and then reloading it!

The js frame thus becomes like a library of sorts that is always accessible
from anywhere within the browser.

I'm sure there are people that won't like this but it works for me & very
well so I might add.
My 2c

Cheers,
Fermin DCG
 
B

Bas Cost Budde

Brian said:
You are correct, but there is one solution that comes to mind (I am sure
there are others)...

myHtml.html:
<script type="text/javascript" src="some.js"></script>

some.js:
function writeScript( JSfile )
{
document.write("<script type='text/javascript' src='" +
JSfile + "'></scr" + "ipt>");
}

writeScript("file1.js");
writeScript("file2.js");
writeScript("file3.js");
...
Now isn't it so that document.write replaces the existing content? That
is rather harsh for what I have in mind.
 
B

Brian Genisio

Now isn't it so that document.write replaces the existing content? That
is rather harsh for what I have in mind.

That is incorrect... in this context. document.write works differently
in two distinctively different contexts:

1. While the page is still parsing... Everything written is appended
after the current script tag, and then parsed into the document

2. After the page has parsed, ie: an action has occured (such as onLoad,
onClick, etc)... In this case, it overwrites everything.

The context I gave, is of type 1. If you do it in inline scripting, you
have nothing to fear.

To clarify:

<SCRIPT ...> document.writeln(...) </SCRIPT>

will append after the script tag

<SCRIPT ...> function ABC(...) { document.writeln(...); } </SCRIPT>
<a href=... onClick="ABC(...)">...</a>

will wipe out the page with the new text, when the link is clicked

Brian
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top