473,394 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Include JavaScript??

JS
I have a rather long JavaScript in my <HEAD> </HEAD> part. I would like to
put this javaScript in another file and then just include it instead. Is
that possible and what should files containing only javascript be called??
Jul 23 '05 #1
10 1313
JS wrote:
I have a rather long JavaScript in my <HEAD> </HEAD> part. I would like to
put this javaScript in another file and then just include it instead. Is
that possible and what should files containing only javascript be called??


<script type=text/javascript src=myJSFile.js></script>

The </script> is important: some older browsers will not understand />,
and a closing of some kind is required.

JavaScript files are best named with a .js extension.

Jul 23 '05 #2
On 30/05/2005 10:41, Random wrote:
<script type=text/javascript src=myJSFile.js></script>
The type attribute contains a slash, so it must be quoted. The src
attribute doesn't need to be quoted in snippet shown above, but there is
no harm in doing so.
The </script> is important
Of course. Closing tags are required for SCRIPT elements.
some older browsers will not understand />, [...]
That's because it creates HTML that is gibberish. I know you're
inferring the use of XHTML, but that isn't viable on the Web anyway.
Moreover, element minimisation for non-empty elements is not permitted
by XHTML 1.0 - Appendix C (though that section is only informative).
JavaScript files are best named with a .js extension.


Unless the server has been configured differently, they should always be
named with a .js extension otherwise the correct content type will not
be sent.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
Michael Winter wrote:
On 30/05/2005 10:41, Random wrote:
<script type=text/javascript src=myJSFile.js></script>
The type attribute contains a slash, so it must be quoted. The src
attribute doesn't need to be quoted in snippet shown above, but there is
no harm in doing so.


Call me a minimalist. I've never seen a single browser that needed
quotes on it-- even Lynx understands barewords, slash or no slash.
Hitting that damned quote key so often hurts my pinky.
The </script> is important


Of course. Closing tags are required for SCRIPT elements.


Go on, really?
some older browsers will not understand />, [...]


That's because it creates HTML that is gibberish. I know you're
inferring the use of XHTML, but that isn't viable on the Web anyway.
Moreover, element minimisation for non-empty elements is not permitted
by XHTML 1.0 - Appendix C (though that section is only informative).


Fortunately or unfortunately, some newer browsers do support minimising
even in HTML. Hence the distinguishment.

JavaScript files are best named with a .js extension.


Unless the server has been configured differently, they should always be
named with a .js extension otherwise the correct content type will not
be sent.


Which is why it is best, but not necessarily required, to use a .js
extension.

Did you have something helpful to add here, Mr. Winter?

Jul 23 '05 #4
On 30/05/2005 13:16, Random wrote:
Call me a minimalist.
There may be better adjectives.
I've never seen a single browser that needed quotes on it [...]
I can't say I've come across a user agent that can't handle tag soup.
That doesn't mean I make a habit of writing it, and I would certainly
never recommend it.

[snip]
Closing tags are required for SCRIPT elements.


Go on, really?


If it's so obvious, why did you even mention it in the first place? I
would hope that the OP knows enough about HTML to understand the
difference between empty and non-empty elements. It just seems that the
OP was unaware of the src attribute, or the perhaps the precise details
of using it. A section of the updated FAQ[1] notes (yet to be
introduced) will include detailed information on this topic.

[snip]
Fortunately or unfortunately, some newer browsers do support minimising
even in HTML. Hence the distinguishment.
Looking here, only Mozilla does. All other user agents I currently have
to hand fail, whether it be in executing the script, parsing subsequent
markup, or both. Therefore, the minimisation of non-empty elements in
HTML should always be avoided; there's no value in making distinctions.

[snip]
Did you have something helpful to add here, Mr. Winter?


Pointing out dubious advice is helpful, in my opinion.

Mike
[1] The current draft:
<URL:http://www.litotes.demon.co.uk/js_info/FAQ8_1_Draft.html>

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #5
Random wrote:
Michael Winter wrote:
On 30/05/2005 10:41, Random wrote:
> <script type=text/javascript src=myJSFile.js></script>
The type attribute contains a slash, so it must be quoted.
The src attribute doesn't need to be quoted in snippet
shown above, but there is no harm in doing so.


Call me a minimalist. I've never seen a single browser that
needed quotes on it-- even Lynx understands barewords, slash
or no slash. Hitting that damned quote key so often hurts my
pinky.


The "I have never seen a browser that ..." argument may just be an
indicator of having not looked at many browsers. The THML specification
is very clear on the circumstances where quotes may be omitted from
attribute values. That browsers are forced, by the prevalence of
incompetent web authoring, to be more tolerant is not an exclude for
encouraging the authoring of incorrect HTML. Among other things, how is
someone going to see the significant problems when validating their HTML
if the validator is spewing out error reports every time it encounters
an attribute value that should be quoted but has not been.
> The </script> is important


Of course. Closing tags are required for SCRIPT elements.


Go on, really?


Yes, they are not optional in the HTML DTDs.

<snip> Fortunately or unfortunately, some newer browsers do support
minimising even in HTML. Hence the distinguishment.

<snip>

Such an HTML browser would be very broken, can you name one?

Richard.
Jul 23 '05 #6
Michael Winter wrote:
On 30/05/2005 10:41, Random wrote:

<snip>
JavaScript files are best named with a .js extension.


Unless the server has been configured differently, they
should always be named with a .js extension otherwise the
correct content type will not be sent.


A server cannot be configured to send JS files with the correct content
type as there is no correct content type for JS. They may be configured
to send JS with an incorrect content type (text/css would be incorrect)
but my experiments, outputting incorrect content types with dynamically
generated JS, suggest that web browsers consistently disregard the
content-type headers sent with JS files, which is reasonable given that
there is no correct content type. (I think that Netscape 2 was the only
browser that had an attitude towards JS content type, and it wanted and
application-x version).

I suspect that character encoding is all the browser will be interested
in when it comes to imported JS files, and whatever shows up will be
passed to the JS interpreter to syntax-error or not (assuming HTTP
status 200). Hence the reoccurring problems with servers sending custom
HTML 404 pages with HTTP 200 statuses making it look like JS is not
working when it is in fact not being found.

Richard.
Jul 23 '05 #7
On 30/05/2005 13:59, Richard Cornford wrote:
Michael Winter wrote:
[snip]
Unless the server has been configured differently, [scripts]
should always be named with a .js extension otherwise the
correct content type will not be sent.


A server cannot be configured to send JS files with the correct content
type as there is no correct content type for JS.


:P True. There are MIME types that are generally understood to be
Javascript (application/x-javascript and text/javascript), but there is
no registered value. I remember the lecture from Jim Ley well. :)
They may be configured to send JS with an incorrect content type [...]


Yes. What I was trying to say is that using a .js extension will send a
reasonable value with a properly configured server, but unless the
server is prepared for such an event, using a different extension will
result in an inappropriate value (possibly one of text/plain or text/html).

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #8
Random wrote:
JS wrote:
I have a rather long JavaScript in my <HEAD> </HEAD> part. I would like
to put this javaScript in another file and then just include it instead.
Is that possible and what should files containing only javascript be
called??

<script type=text/javascript src=myJSFile.js></script>

The </script> is important: some older browsers will not understand />,


It is a matter of the markup language, not of the age of the browser.
`/>' (XML NET/Web Annex NESTC-NET) is not a tag close delimiter in HTML,
only in XHTML[1]. Instead, `/' in a HTML start tag, as introducing an
SGML SHORTTAG feature not widely implemented but commonly misused in
cluelessly called "HTML compatible XHTML" alas proposed by the XHTML 1.0
Specification, serves a completely different purpose:

<http://www.dodabo.de/html+css/tests/shorttag.html>
PointedEars
___________
[1] ref. HTML 4.01, chapter 20,
and <http://www.w3.org/TR/NOTE-sgml-xml.html>
Jul 23 '05 #9
Richard Cornford wrote:
Random wrote:
Fortunately or unfortunately, some newer browsers do support
minimising even in HTML. Hence the distinguishment.
[...]
Such an HTML browser would be very broken,


It would not, iff it properly implements HTML SHORTTAG features.
can you name one?


See the last URI in my other followup.
PointedEars
Jul 23 '05 #10
Thomas 'PointedEars' Lahn wrote:
Richard Cornford wrote:
Random wrote:
Fortunately or unfortunately, some newer browsers do support
minimising even in HTML. Hence the distinguishment.
[...]


Such an HTML browser would be very broken,


It would not, iff it properly implements HTML SHORTTAG
features.

<snip>

If you didn't backtrack this thread how did you know what Random was
referring to as 'minimising', and if you did backtrack the thread how
come you did not observe that Random's comment was in response to Mike's
comments relating to XHTML 'minimising'? And an HTML browser that
interprets that style of syntax in the way an XHTML browser would is
broken.

But we still have no evidence of any HTML browsers interpreting <script
.... /> in an XHTML style so this is academic.

Richard.
Jul 23 '05 #11

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

Similar topics

21
by: Peter Bassett | last post by:
I have a .shtml file, that displays photos, in which I wish to pass some functionality off to an Include file for reusability purposes. Unfortunately, it's not working. Here is a portion of the...
22
by: Weston C | last post by:
I know of course that you can use <script src=""></script> in an HTML document to include javascript code you don't want to spill all over the page. I'm wondering if there's a way to include...
6
by: Jamie | last post by:
Hi there, I have a bit of java script which I would like to include in all of my html pages. Is there an easy way to include it with a reference or something. I don't want to have to copy...
7
by: Fabri | last post by:
I'm trying to develop a way to include static files in htm pages with javascript. I'm trying to use XMLHTTP object this way: ...
5
by: Tony Strazzeri | last post by:
Hi all, I a fairly new to html and Javascripting. I have been trying to write some code to hide my email address from spam harvesters. I copied the code from various web examples and modified...
3
by: Brit | last post by:
Hello, I have been trying to include a js file from within another js file. I can get this to work for firefox, but not i.e. What I have is a.html ... <script language='JavaScript'...
1
by: Carafound | last post by:
Help!!! I am trying to write an ASP include statement <!--#include file="file.asp"--to an HTML stream in a new window using Javascript document.write command. The page gets built however the...
2
by: Rolf Welskes | last post by:
Hello, I have simple in one folder FolderA MyMaster.master and script01.js styles01.css The content pages are in
2
by: William van Zwanenberg | last post by:
Hi there, I wonder if you guys can help. I'm currently experiencing some coding problems what with trying to use PHP and javascript in tandem. What I'm ultimately trying to achieve is use...
4
by: rocketeer | last post by:
I've a set of Javascript classes that maintain state. For example, gm.js might be: var GroupManager { groups: {} }; Over time I add new groups to the list: GroupManager.groups = myGroup; ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.