javascript include files?

S

skijor

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
 
P

Peter Michaux

skijor said:
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
 
S

skijor

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.
 
R

Richard Cornford

Peter Michaux wrote:
<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.
 
P

Peter Michaux

Richard said:
Peter Michaux wrote:


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.

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.

<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
 
R

Richard Cornford

Peter said:
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.

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.
 
V

VK

Richard said:
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.
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>
 
P

Peter Michaux

VK said:
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?...6af731346f78.cccd4aa02a3993ab06e56af731346f78

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
 
V

VK

Peter said:
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?...6af731346f78.cccd4aa02a3993ab06e56af731346f78>

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).
 
R

Richard Cornford

VK said:
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).

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.
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/mozilla.dev.tech.js-engine/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.
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top