473,671 Members | 2,454 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 1851

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 "applicatio n/javascript" from
Bjoern Hoerhrmann floating around. Despite his RFC is gone, the
confusion remained. See more at "script type=?" thread
<http://groups.google.c om/group/mozilla.dev.tec h.js-engine/browse_frm/thread/93f705444dd4acc 5>

And to really spice the things up there is long existing
"applicatio n/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 "applicatio n" 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 "applicatio n/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
"applicatio n/javascript" and he also received approval for
"applicatio n/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
"applicatio n/javascript" and he also received approval for
"applicatio n/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_ccc d4aa02a3993ab06 e56af731346f78. cccd4aa02a3993a b06e56af731346f 78>

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.c om/group/mozilla.dev.tec h.js-engine/browse_frm/thread/93f705444dd4acc 5>
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.styleS heet - 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
"applicatio n/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/93f705444dd4acc 5>
<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
1991
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 until I hosted them on our IIS server. Then the pages refuse to see nested javascript calls. e.g.
0
1185
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 onLoad of the body tag anyway to start my JS function. Anyway, I changed the order of my includes such that the one that calls all the rest was loaded last and that worked.....or so I thought.
9
13399
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"> However, I have found that I can use an alternate file extension, such as <script src="foo.HTML" type="text/javascript"> It works fine with my IE 6 and Mozilla. Will it work with other browsers?
9
6126
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 for easy editing. I'm hoping there is some sort of include or import directive that I could use. Or if no such directive exists, I'm wondering if anyone has written one which I could use. I need to do this without any server side scripting....
3
2570
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" --> Is there a similar way in ASP.NET? Thanks, Justin.
3
2748
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 to the site, the page is displayed without a stylesheet. If I continue to use the app, general Javascript Errors like "Object Expected" start occurring. If I click refresh, the page and the stylesheet displays fine and there are no Javascript...
7
1945
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 when I run on a client's corporate server. I have no problems on a laptop connected to the network or on my home PC. Any ideas? Thanks in advance.
2
4258
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 functioning as desired when I test it on my machine using Visual Studio 2005. From what I have determined, the files I am forgetting are the JavaScript files from the Microsoft AJAX Library (See "To install Microsoft AJAX Library" at the bottom of...
0
8825
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8605
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8676
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6237
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5703
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4416
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.