JavaScript Examples with Code: Hello, World in JavaScript

SCRIPT tag

You can insert JavaScript programs anywhere in HTML with the help of the SCRIPT tag.

For example:

<!DOCTYPE HTML>
<html>

<head>
  <meta charset="utf-8">
</head>

<body>

  

Beginning of the document

  <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%3E%0A%20%20%20%20alert(%20'Hello%2C%20world!'%20)%3B%0A%20%20%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;script&gt;" title="&lt;script&gt;" />

  

End of the document


</body>

</html>

This example uses the following elements:

<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%3E%20...%20%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;script&gt;" title="&lt;script&gt;" />

Script tag contains executable code. Previous HTML standards required mandatory indication of the attribute type, but now it is no longer needed. Simple <script> is enough.

When the browser indicates <script>:

  1. It displays the page until the <script> tag is indicated.
  2. It switches to the Javascript mode and executes the content.
  3. When the execution is finished, the browser returns to the HTML mode and displays the remaining part of the document.

Modern markup for SCRIPT

In the old design scripts, SCRIPT tag was a bit more complicated. The following elements can be found in the older guidelines:

<script type=...>attribute

Unlike HTML5, standard HTML 4 requires mandatory indication of that attribute: type = “text / javascript”. If you specify a different type value, the script will not be executed.

The type attribute is not required in modern development.

<script language=...>attribute

This attribute is used to specify the language in which the script is written. Now JavaScript is a default language, so this attribute is not necessary.

Comments before and after the scripts

In very old books and manuals, sometimes you can find a recommendation to use HTML-comments inside the SCRIPT, to hide Javascript from the browsers that do not support it.

It looks like this:

<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20type%3D%22text%2Fjavascript%22%3E%3C!--%20...%20%2F%2F--%3E%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;script&gt;" title="&lt;script&gt;" />

Netscape, the browser for which these tricks were created, is dead. Therefore, there is no need for these comments.

So, to insert the script, we just write <script>, with no additional attributes and comments.

Thanks for your attention!

The presented assignment is one of many JavaScript samples done by our expert authors. We wish to remind you that you are not allowed to use it in any way that may violate the writer’s rights, including citing parts of the paper without proper references or claiming it to be your own. If you need help with a difficult programming assignment, you can either check our blog and go through other JavaScript examples with code or place your order to get such examples custom-written for you.

How can you place your order? Follow the short order from presented above – specify the type of the assignment and the subject, and then you will be automatically redirected to a longer form. You can then specify your requirements and pinpoint all the additional information you need your expert to consider while working on your JavaScript examples with code. You can track the whole process from your personal page, and you can also chat with your expert in case you have some questions regarding the details of your assignment. Keep in mind that you will be asked to pay only after you confirm that you are fully satisfied with the quality of the order, and no earlier. Programming can be difficult, but we know how to make it easy.

Leave a Reply

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

Customer testimonials

Submit your instructions to the experts without charge.