473,513 Members | 2,430 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

javascript include files?

I am converting a raw html webiste to use javascript. A lot of the
html code is duplicated in many pages. Some of that code can be
consolidated into javacript but I don't want to add the same functions
to every page. Is there a technique to keep the functions in one file
like a header file in compiled programming languages and just include
them somehow?

-- regards
-- note: email address is never checked

Dec 9 '06 #1
9 1844

skijor wrote:
I am converting a raw html webiste to use javascript. A lot of the
html code is duplicated in many pages. Some of that code can be
consolidated into javacript but I don't want to add the same functions
to every page. Is there a technique to keep the functions in one file
like a header file in compiled programming languages and just include
them somehow?
It sounds like you want to use JavaScript to generate HTML. This may
not be wise if you want browsers with old or no JavaScript to be
supported. However...

You can use the script tags to include a JavaScript file

<script src="path/to/myScript.js" type="text/javascript"></script>

Although I hear from Douglas Crockford the type attribute is not
necessary and ignored by browsers in favor of the MIME type the server
sends with the actual file. So it reduces to the following

<script src="path/to/myScript.js"></script>

http://w3schools.com/tags/tag_script.asp

If you are new to JavaScript you may want to get a book.

http://jibbering.com/faq/#FAQ3_1
http://jibbering.com/faq/newfaq/#FAQ3_1

be wary of errata reported or not.

Peter

Dec 9 '06 #2
It sounds like you want to use JavaScript to generate HTML.

no. I was going to update a dreamweaver template file with javascript
functions. That would have included the function definitions in about
45 files. Your example below worked fine and now the impact is much
less since the files were updated with just the single include
statement and not the actual functions.
<script src="path/to/myScript.js"></script>
Dec 9 '06 #3
Peter Michaux wrote:
<snip>
<script src="path/to/myScript.js" type="text/javascript">
</script>

Although I hear from Douglas Crockford the type attribute
is not necessary
The TYPE attribute is required in formally valid HTML That is; without a
TYPE attribute it will not be possible to get an HTML 4 validator to
confirm the document as valid.
and ignored by browsers
It is certainly true that older browsers do not know about the TYPE
attribute and do ignore it. More modern browsers do recognise and attach
meaning to the value of that attribute.
in favor of the MIME type the server
sends with the actual file.
<snip>

That is completely untrue. A number of years ago I experimented with
sending diverse MIME types in content-type headers with javascript files
and found no evidence that any browsers of the time paid any attention
to the value used. Browsers may have an interest in character encoding
declared in such headers (for obvious practical reasons) but it appeared
that regardless of the MIME type whatever was send was interpreted as
javascript source code text. Indeed, over the years a common resolution
to "my imported script doesn't work" questions has been faulty URLs
being used in the SCRIPT element's SRC attributes and the response being
sent by the server being an HTML 'error page', with the resulting syntax
error at line 1 being the symptom of the inappropriate interpretation of
the document.

Richard.

Dec 10 '06 #4
Richard Cornford wrote:
Peter Michaux wrote:
<snip>
<script src="path/to/myScript.js" type="text/javascript">
</script>

Although I hear from Douglas Crockford the type attribute
is not necessary

The TYPE attribute is required in formally valid HTML That is; without a
TYPE attribute it will not be possible to get an HTML 4 validator to
confirm the document as valid.
I just tested the w3c validator and of course you are correct.

I am glad I didn't state it as fact. Perhaps I've learned that all
JavaScript related information really does have to be verified.

and ignored by browsers

It is certainly true that older browsers do not know about the TYPE
attribute and do ignore it. More modern browsers do recognise and attach
meaning to the value of that attribute.
in favor of the MIME type the server
sends with the actual file.
<snip>

That is completely untrue. A number of years ago I experimented with
sending diverse MIME types in content-type headers with javascript files
and found no evidence that any browsers of the time paid any attention
to the value used.
Very interesting. Thanks for the info. I wonder if things have changed
in the last few years. Either way, it doesn't really matter because I
would like my documents to validate and I imagine including the type
attribute can only act as a safety net.

Peter

Dec 10 '06 #5
Peter Michaux wrote:
Richard Cornford wrote:
>Peter Michaux wrote:
<snip>
<script src="path/to/myScript.js" type="text/javascript">
</script>

Although I hear from Douglas Crockford the type attribute
is not necessary

The TYPE attribute is required in formally valid HTML That is;
without a TYPE attribute it will not be possible to get an HTML 4
validator to confirm the document as valid.

I just tested the w3c validator and of course you are correct.

I am glad I didn't state it as fact.
But it is a fact, both specifically (assuming your hearsay report of
what Douglas said is accurate) and generally: it is not necessary to
include a TYPE attribute in a SCRIPT opening tag. It is just also true
that it is not possible to omit the TYPE attribute and create a formally
valid HTML document. Neither excludes the other because they speak of
different things.
Perhaps I've learned that all JavaScript related
information really does have to be verified.
That will do you no harm.

<snip>
>>in favor of the MIME type the server
sends with the actual file.
<snip>

That is completely untrue. A number of years ago I
experimented with sending diverse MIME types in
content-type headers with javascript files and found
no evidence that any browsers of the time paid any
attention to the value used.

Very interesting. Thanks for the info. I wonder if things
have changed in the last few years.
A very significant change happened this year, in that official MIME
types for javascript were introduced (after an absence of 10 years,
which seems to explain why browsers were not interested in what MIME
type appeared in content type headers).
Either way, it doesn't really matter because I
would like my documents to validate and I imagine
including the type attribute can only act as a safety
net.
It is probably also a very good idea to send an appropriate MIME type in
content-type headers along with imported javascript. They may be ignored
at present but now there are official values the likelihood is that the
future will see browsers that take notice of them. And anyway, doing the
whole job, in sending content over HTTP to a client, isn't finished
until you are sending appropriate HTTP (including content-type) headers
with everything. Leaving it to a blend of random chance and the default
configuration of servers is just not professional.

Richard.
Dec 10 '06 #6
VK

Richard Cornford wrote:
But it is a fact, both specifically (assuming your hearsay report of
what Douglas said is accurate) and generally: it is not necessary to
include a TYPE attribute in a SCRIPT opening tag.
With a regular notion of a IE-targeted HTML page using both VBScript
and JScript: in this case "language" or "type" indication is obligatory
or the scripts may crash.
Perhaps I've learned that all JavaScript related
information really does have to be verified.
That will do you no harm.
Very true.
A very significant change happened this year, in that official MIME
types for javascript were introduced.
Such as? IMO the confusion became only bigger because besides the
traditional "text/javascript" there is "application/javascript" from
Bjoern Hoerhrmann floating around. Despite his RFC is gone, the
confusion remained. See more at "script type=?" thread
<http://groups.google.com/group/mozilla.dev.tech.js-engine/browse_frm/thread/93f705444dd4acc5>

And to really spice the things up there is long existing
"application/x-javascript" on Windows platforms with Internet Explorer
installed. This MIME is used to run binary compiled HTC behaviors,
otherwise they will be ignored. In this case "application" part is
indeed applicable. AFAIK there is a huge confusion about the nature and
the purpose of this MIME even amond well informed people, see for
instance: <http://annevankesteren.nl/2005/02/javascript-mime-type>

Dec 10 '06 #7
VK wrote:
>
Richard Cornford wrote:
A very significant change happened this year, in that official MIME
types for javascript were introduced.

Such as? IMO the confusion became only bigger because besides the
traditional "text/javascript" there is "application/javascript" from
Bjoern Hoerhrmann floating around. Despite his RFC is gone, the
confusion remained. See more at "script type=?" thread
Douglas Crockford said that the official MIME is
"application/javascript" and he also received approval for
"application/json"

The source for these Douglas Crockford bits of info are buried in six
longish videos on the Yahoo! site where Douglas teaches internal Yahoo!
staff about JavaScript.

http://video.yahoo.com/video/group?g...e56af731346f78

I thought the first video of the "Advanced JavaScript" serious was
especially good and anyone learning about prototypes would benefit from
watching it (and all the others).

Peter

Dec 10 '06 #8
VK
Peter Michaux wrote:
Douglas Crockford said that the official MIME is
"application/javascript" and he also received approval for
"application/json"

The source for these Douglas Crockford bits of info are buried in six
longish videos on the Yahoo! site where Douglas teaches internal Yahoo!
staff about JavaScript.
<http://video.yahoo.com/video/group?gid=g_cccd4aa02a3993ab06e56af731346f78.cccd4 aa02a3993ab06e56af731346f78>

Well, Mr.Crockford rather extensively "beautified the reality" :-)
which is normal though in the teaching process: your students are
coming to learn the facts, not to learn the opinions' mishmash - the
latter is for the labs (and the Usenet :-).

He was definitely referring to these two requests for comments (RFC):

1) already mentioned Hoehrmann's RFC #4329
<http://www.ietf.org/rfc/rfc4329.txt>
("- How much troubles one man can make? - It is the question of
`talents'." :-(

2) his own (Crockford's) RFC #4627
<http://www.ietf.org/rfc/rfc4627.txt>
See again the thread
<http://groups.google.com/group/mozilla.dev.tech.js-engine/browse_frm/thread/93f705444dd4acc5>
for further inspirations :-)
P.S. When posting a long URL, it is suggested to wrap it into
lesser-than ... greater-than signs. This way the link will not get
broken on HTML-enabled viewers (like Outlook Express or Google Groups
via browser).

Dec 10 '06 #9
VK wrote:
Richard Cornford wrote:
>But it is a fact, both specifically (assuming your hearsay
report of what Douglas said is accurate) and generally:
it is not necessary to include a TYPE attribute in a SCRIPT
opening tag.

With a regular notion of a IE-targeted HTML page using both VBScript
and JScript: in this case "language" or "type" indication is
obligatory or the scripts may crash.
You have never grasped the distinction between what is termed "crashing"
and a syntax error.

Observing that there are circumstances where some attributes may be
necessary does not alter the truth that TYPE attributes are not
necessary (as those circumstances are not inevitable).

>>Perhaps I've learned that all JavaScript related
information really does have to be verified.
>That will do you no harm.

Very true.
Then why don't you then? But then I recall that incident here you
declared that the suggestion that Mozilla supported -
document.styleSheet - was a delusion following from reading too many
specifications instead of testing for yourself, and then when on to
explain that you had tested that feature of Mozilla for a month and
concluded that it did not support the feature. With your style of
practical verification resulting in you knowing less as a result than
you did to start with I can see why you show no evidence of ever doing
so.
>A very significant change happened this year, in that official
MIME types for javascript were introduced.

Such as? IMO the confusion became only bigger because besides
the traditional "text/javascript" there is
"application/javascript" from Bjoern Hoerhrmann floating around.
That is now the official MIME type for javascript (text/javascript is
also an official MIME type but was deprecated at the point of inclusion
in the standard).
Despite his RFC is gone, the
confusion remained. See more at "script type=?" thread
<http://groups.google.com/group/mozil.../browse_frm/th
read/93f705444dd4acc5>
<snip>

That is just you making a fool of yourself in public again. you can
almost here the jaws of the regulars on that group dropping at your
obtuse stupidity.

Richard.
Dec 10 '06 #10

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

Similar topics

25
1970
by: Martin Walke | last post by:
Hi, This may seem like a client side problem but.... I have web pages that are made up of a number of javascript include files as well as 'in page' script. They all work fine running locally...
0
1179
by: Martin Walke | last post by:
Hi guys, For those who responded before here's an update from hell!!! I think I confused people before when I said on the 'page load' and Chris suggested using the body onload. I was using the...
9
13374
by: David D. | last post by:
Does the file extension matter when including a JavaScript file in an HTML page? Normally, one would include a JavaScript file in an HTML page using <script src="foo.JS" type="text/javascript">...
9
6116
by: Trenqo 0 | last post by:
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...
3
2561
by: Justin | last post by:
I am using asp.net with a C# code behind and I need to figure out how to include a javascript file for use with my code. It was simple to do this in classic ASP: <!-- #include File="md5.js" --> ...
3
2744
by: fbwhite | last post by:
I am having an issue (maybe two issues) in my application regarding a stylesheet not loading and I am getting Javascript Errors. If I clean out a client's temporary internet files and then browse...
7
1933
by: McKirahan | last post by:
Has anyone experienced this (and found a solution)? I have five files: a .htm that invokes four .js include files. Only the first alert in the first include is displayed. This only happens...
2
4240
by: Nathan Sokalski | last post by:
I am moving my website from my machine to my webhost, and need some help with what extra files I need to include due to the fact that I used AJAX in my site. Everything on the site is obviously...
0
7265
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
7171
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...
1
7111
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5692
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5095
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3240
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3228
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
461
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.