Introduction to JavaScript

April 26, 2019 Java

What is a scripting language?

Scripting languages, also called script languages, are programming languages that are interpreted or compiled each time they are executed. The scripts are executed directly from their source code, which is usually text files that contain specific language marks.

Therefore, “scripts” are often treated as distinct from “programs”, which are usually compiled from the source code into binary executable files only after they are changed, and then run from these binary files without needing the source code. The scripts were created to shorten the traditional editing-compilation-link-execution process.

What is JavaScript?

JavaScript is a scripting language that is most often used for web development on the client side. The client-side refers to the operations performed by the client (in our case, the client is the browser) in a client-server relationship.

Despite the name, JavaScript is not essentially related to the Java programming language.

  • JavaScript was designed to add interactivity to HTML pages
  • A scripting language is a light programming language.
  • A JavaScript consists of lines of executable computer code.
  • A JavaScript is usually integrated directly into the HTML pages
  • JavaScript is an interpreted language (means that the scripts run without preliminary compilation)

What can a JavaScript do?

  • JavaScript gives HTML designers a programming tool: HTML authors are not normally programmers, but JavaScript is a scripting language with a very simple syntax! Almost anyone can put small “fragments” of code in their HTML pages
  • JavaScript can place dynamic text in an HTML page: a JavaScript statement like this: document. write (“<h1>” + name + “</ h1>”) can write a variable text in an HTML page
  • JavaScript can react to events: you can configure JavaScript to run when something happens, such as when a page has finished loading or when a user clicks on an HTML element
  • JavaScript can read and write HTML elements: a JavaScript can read and change the content of an HTML element
  • JavaScript can be used to validate data: a JavaScript can be used to validate form data before sending it to a server. This saves the extra processing server
  • JavaScript can be used to detect the visitor’s browser: a JavaScript can be used to detect the visitor’s browser and, depending on the browser, load another page specifically designed for that browser
  • JavaScript can be used to create cookies: a JavaScript can be used to store and retrieve information on the visitor’s computer

“Hello World” Script

<script type="text/javascript">
            document.write("Hello World!")
</script>

The following is the result of placing the previous script on this page:

Hello the world

<Script> tag

The script tag tells the browser where to start the scripts (<script type = “text / javascript”>) and ends (</ script>). The type attribute specifies the type of scripting language that we are using. In our JavaScript example, the type is the same as “text/javascript”.

document.write

The document.write is a standard JavaScript command to write results per page.

Complete the semicolon statements?

Generally, a semicolon is optional! However, semicolons are necessary if you want to place more than one sentence on a line.

Manage browsers that do not support JavaScript

Browsers that do not support JavaScript will show the script as the content of the page. To avoid this, we can use HTML tag labels:

<script type="text/javascript">
  <!--
    document.write("Hello World!")
  //-->
</script>

This will allow browsers not enabled with JavaScript to ignore everything that is inside <! – and ->. The JavaScript-enabled browsers in this section know that they are ignoring <! – that marks the beginning of an HTML tag while // (commenting a JavaScript line) before – – ensures that the browser does not • Try to interpret -> as a command class or JavaScript symbol.

Label <noscript>

It is also good to show a warning to users who visit your JavaScript-enabled website with a browser other than JavaScript. To display a message of this type only for those users of the network, and not for those who have a browser that supports JavaScript, the <noscript> element can be used as follows:

<noscript>This web page uses JavaScript. To get the full effect, you need to use a web browser with a JavaScript capability and set the browser to have

JavaScript enabled.

</noscript>

Become a Java Expert with Certification in 25Hours

Where to put JavaScript

The JavaScript on the page will run as soon as the page loads in the browser. This is not always what we want. Sometimes, we want to run a script when the page loads, the second time a user triggers an event.

Scripts in the header section

The scripts that will be executed when you call them or when the event is activated, go to the header section. When you place the script in the header section, it will make sure that the script loads before someone can use it.

<html>
<head>
<script type=”text/javascript”>
….
</script>
</head>

Become Java Certified Expert in 25Hours

Scripts in the body section

Scripts to be executed when the page is loaded in the body section. When you place the script in a part of the body, it generates the content of the page.

<html>
<head>
</head>
<body>
<script type="text/javascript">
 ....
</script>
</body>

Scripts in the body and head

You can place an unlimited number of scripts in the document, so you can have scripts both in the body and in the head.

<html>

<head>

<script type=”text/javascript”>

….

</script>

</head>

<body>

<script type=”text/javascript”>

….

</script>

</body>

Get java online Training

Using external JavaScript

Sometimes you may want to run the same JavaScript on multiple pages without having to write the same script on the same page.

To make this simpler, you can write JavaScript in an external file. Save an external JavaScript file with an .js extension.

Note: The external script cannot contain the <script> tag!

To use an external script, point to the .js file in the “src” attribute of the <script> tag:

<html>

<head>

<script src=”xxx.js”></script>

</head>

<body>

</body>

</html>

To getting expert-level training for Data Science Training in your location –Java Training in Tambaram | Java Training in Chennaijava Training in Bangalore | Java Training in Pune

 

 

 

 

Leave a Reply

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