By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,516 Members | 1,126 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,516 IT Pros & Developers. It's quick & easy.

How to create JS includes

P: n/a
Hello,

This should be a simple question to anyone who knows JavaScript, but I'm new
to it.

I have a JavaScript that I need to use in many pages. How do I create JS
includes?

I tried the standard method - created a text file, then includud it like
this:

<!-------------#Include File="includes/js.txt"------------------->

but this did't work for me.

I would appreciate your advice.

Thank you,

--
Peter Afonin
Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Peter this is how:

<html>
<head>
<title>Test!</title>
<!-- now include script tag with src attribute pointing to -->
<!-- to your script -->
<script language="javascript" type="text/javascript" src="test.js">
<!-- //

// -->
</script>
</head>
<body>
Hello World!
</body>
</html>

Peter Afonin wrote:
Hello,

This should be a simple question to anyone who knows JavaScript, but I'm new
to it.

I have a JavaScript that I need to use in many pages. How do I create JS
includes?

I tried the standard method - created a text file, then includud it like
this:

<!-------------#Include File="includes/js.txt"------------------->

but this did't work for me.

I would appreciate your advice.

Thank you,

Jul 23 '05 #2

P: n/a
Thank you!

I assume that test.js is just a text file that I assign a "js" extension,
correct?

Peter

"Cycloneous Echevarria" <or*****@engr.uconn.edu> wrote in message
news:40**************@engr.uconn.edu...
Peter this is how:

<html>
<head>
<title>Test!</title>
<!-- now include script tag with src attribute pointing to -->
<!-- to your script -->
<script language="javascript" type="text/javascript" src="test.js">
<!-- //

// -->
</script>
</head>
<body>
Hello World!
</body>
</html>

Peter Afonin wrote:
Hello,

This should be a simple question to anyone who knows JavaScript, but I'm new to it.

I have a JavaScript that I need to use in many pages. How do I create JS
includes?

I tried the standard method - created a text file, then includud it like
this:

<!-------------#Include File="includes/js.txt"------------------->

but this did't work for me.

I would appreciate your advice.

Thank you,

Jul 23 '05 #3

P: n/a

"Peter Afonin" <pa**@specialtypulltabs.com> wrote in message
news:10*************@corp.supernews.com...
Thank you!

I assume that test.js is just a text file that I assign a "js" extension,
correct?


That is correct.
Jul 23 '05 #4

P: n/a
Thank you!

Peter

"Murray" <pa***@SPAMoffSPAMMER.optusSP4Mnet.com.au> wrote in message
news:40***********************@news.optusnet.com.a u...

"Peter Afonin" <pa**@specialtypulltabs.com> wrote in message
news:10*************@corp.supernews.com...
Thank you!

I assume that test.js is just a text file that I assign a "js" extension, correct?


That is correct.

Jul 23 '05 #5

P: n/a
Ivo
> Peter Afonin wrote:
I have a JavaScript that I need to use in many pages. How do I create JS
includes?

"Cycloneous Echevarria" topposted Peter this is how:

<html>
<head>
<title>Test!</title>
<!-- now include script tag with src attribute pointing to -->
<!-- to your script -->
<script language="javascript" type="text/javascript" src="test.js">
<!-- //

// -->
</script>
</head>
<body>
Hello World!
</body>
</html>


A nice bare-bones example. Two remarks on the side: The script element has
no language attribute. It may have had one in the past, but that is history.
To reduce unnecessary webtraffic and in order to "validate", you should
remove the portion which reads <... language="javascript" ...> from the
script tag. Also, the custom of hiding script from older browsers by
encapsulating the script's content in <!-- --> SGML comments is obsolete.
These "older browsers" that do not know about the script tag, are history
too.

You can also make the src point to a script on another website. Socalled
hit-counters often do this. Simply by entering the whole address, including
http protocol. Just be aware that cross-domain security restrictions apply.

As a last remark, a script element that has a src defined, will ignore any
content inside the tag itself. Only if the source file is not found or there
some other error loading the external script, will the engine consider and
execute any script hardcoded inside the tag.

HTH
Ivo
Jul 23 '05 #6

P: n/a
JRS: In article <10*************@corp.supernews.com>, seen in
news:comp.lang.javascript, Peter Afonin <pa**@specialtypulltabs.com>
posted at Fri, 7 May 2004 09:17:37 :
I have a JavaScript that I need to use in many pages. How do I create JS
includes?

I tried the standard method - created a text file, then includud it like
this:

<!-------------#Include File="includes/js.txt"------------------->

but this did't work for me.


Bearing in mind where much system software is written, you should not
expect just one standard for anything.

See <URL:http://www.merlyn.demon.co.uk/js-nclds.htm>.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #7

P: n/a
JRS: In article <40***********************@news.wanadoo.nl>, seen in
news:comp.lang.javascript, Ivo <no@thank.you> posted at Fri, 7 May 2004
22:16:57 :
Also, the custom of hiding script from older browsers by
encapsulating the script's content in <!-- --> SGML comments is obsolete.
These "older browsers" that do not know about the script tag, are history
too.


You are here making the assumption that the script will be read only by
browsers; that is no more than an assumption.

The master copies of my own web pages are most frequently read by a
tool, CHEKLINX, which is affected by seeing these comment delimiters;
without them, the normally-satisfactory javascript if (b<a) needs
to be re-written as if (b< a) or if (b < a ) .

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.