Starting a JavaScript

How do you tell the browser that a script is starting? Moreover, how does he know what kind of script is starting? What if the browser doesn’t support JavaScript?

This is another fragment from the book I started some time ago, named ‘JavaScript class’

Telling the browser JS is starting

We can’t tell the browser that a JavaScript is starting using some JavaScript code, because he doesn’t know, how to interpret it. We should tell the browser that a JavaScript code is starting that it says that another JavaScript is starting. That’s a paradox. Therefore, we must tell him using some usual code that all browsers can understand. That is HTML. We will use a HTML tag to tell the browser that JavaScript code is starting, and to send it to the JavaScript compiler implemented in the browser:

<script language=”JavaScript1.5” type=”text/javascript”>

Let’s take cut apart the code. It start with the usual ‘<’ HTML sign used to denote that a tag is starting. Then it uses ‘script’ to tell the browser a script is starting. ‘language=”JavaScript1.5”’ attribute is used to tell the browser more information about the script. And another attribute, ‘type=”text/javascript”’ means the same as the language attribute, but is used for compatibility reasons. Then the ‘>’ HTML tag-closing sign is used for telling the browser that the declaration is finished, but the script is just starting.
As any non-empty HTML tag, JavaScript tag has a closing tag, . So, the skeleton of our script looks like this until now:

<script language=”JavaScript1.5” type=”text/javascript”>
</script>

Language attribute

You saw in our script that we used an attribute that specified the language, and the language version: ‘language=”JavaScript1.5”’. We talked earlier in this book about JavaScript versions, and now we’re already applying the stuff we talked about. In the tag in which we are declaring that the script is starting, we can declare, as an attribute, the version that we use. If we specify an older version, for example JavaScript 1.3, the syntax will be checked as with an older interpreter, so if you use functions that are implemented in version 1.4 or earlier, the browser will most probably throw an error, even if it supports version 1.5.
Anyway, you’ll probably use the most recent version in your scripts, 1.5.

Pay attention if you want to use the first version of JavaScript, you must not write:

<script language=”JavaScript1.0” type=”text/javascript”>

The correct form is:

<script language=”JavaScript” type=”text/javascript”>

Don’t specify a version at all.

Two attributes for compatability
You saw that we used two attributes that do the same thing, and I said we must use them for compatibility:

language=”JavaScript”
type=”text/javascript”

Because none of this two attributes were marked as a standard, it’s better to use both of them. If you are sure that your target users use only Internet Explorer, for example, you can use only one of the attributes, because IE understands both of them. And that’s the ‘type=”text/javascript”’ attribute, because in the new XHTML language, the language attribute has been deprecated. I recommend you to use only this attribute; don’t waste your time with the language attribute.
For the start, we will use in our script both attributes, just for you to get familiar with them, but we will deprecate them on the road.

Hidding JS from older browsers

Yes, this is about compatibility again.
In the first days of JavaScript, when not all browsers had built-in support for understanding JavaScript, this code was very important. Now, because most of the browsers support JavaScript, and when I say most, I mean nearly every browser, you can safely discontinue using this improvised code. Let’s see how we can hide JS from old versions of browsers:

<!--
//-->

Yeah, it’s a HTML tag that represents a comment. Actually, it’s not entirely a HTML comment tag. It’s a HTML comment tag and two slashes, which represents a JavaScript comment. So, it’s a HTML comment combined with a JavaScript comment. You’ll learn more about comments in the upcoming lessons.
If the browser understands JavaScript, he will pass the comment, even if it’s commented with the HTML tags. If it doesn’t understand JavaScript, it will think that a comment is a comment, so he will not process it.
Older JavaScript programmers made a habit to comment their script, but now it’s no need too. Anyway, we will use it in our first examples, for the same reasons we use both declaration attributes, for you to get familiar.

Script skeleton

Here’s a very very very simple JavaScript script:

<html>
<head>
<title>Script skeleton</title>
<body>
<script language=”JavaScript1.5” type=”text/javascript”>
<!--
//-->
</script>
</body>
</html>

I call it very very very simple because it does nothing! Actually, this is just the skeleton that you must use every time you will write your script, except the comments, which we discussed earlier that are optional. You can write your script in an editor but it will do nothing.

Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top