The Best Technical Blog Ever

twitterrss

Introduction to JavaScript

JavaScript was released by Netscape and Sun Microsystems in 1995. Javascript is a scripting language that will allow client-side script to interact with the user and make dynamic pages based on the user's choices.


It adds interactivity to the website such as validates user input when the user submits the form and responses to button clicks. Eventually as you get more practised with the language you'll be able to create games, animated 2D and 3D graphics, and more! This tutorial helps you get started with this exciting language and covers all the core things you need to know before you can build and customize your own scripts.


To start javascript, all you need is a simple text-editor such as Notepad and a browser. JavaScript code is usually embedded in the HTML, to be interpreted and run by the client's browser. JavaScript statements should ends with a semi- colon and is case sensitive.


Writing JavaScript with script tag:

You can add JavaScript commands in three ways.


  • Embedding code
  • Inline code
  • External file

JavaScript code is typically embedded within an HTML page using the JavaScript tag <script>. Since you will need to let the browser know in advance when you enter javascript to an HTML page. This tag alerts a browser that JavaScript code follows. You can set the <script> tags, anywhere within your web page, however it is usually advisable to keep as much as possible in the <head> section.


<script type="text/javascript">
	//JavaScript code goes here
</script>
Example:
js demo

Output:
Welcome to Solsoftsys... JavaScript tutorial!  

The document.write tells the browser that what follows within the parentheses is to be written into the document. The script tag takes two main attributes:


  • language: This attribute specifies what scripting language you are using.

  • type: alerts the browser to the type of scripting language it is about to deal with (there are others, like VBScript), and so helps it to interpret the code. Here, value should be set to "text/javascript".

Example :
js demo

Output:
Welcome to Solsoftsys!!!
JavaScript tutorial!!!
Greetings... 

An interesting feature is that you can write all kinds of HTML tags to webpages with the document.write method. In the same way, if you want to have a script run when a user clicks the button, then you have to place that script in the head.


Example :
js demo

Output :


Sometimes you have a block of code(script) which you will want to use on several different web pages, then you should place that block in an external Javascript file. It is useful to group common functions in an external JavaScript file. This allows the reuse of the functions in the file in multiple HTML pages. Here, JavaScript functions are stored in a file with .js extension.


Adding an external file is basically the same as doing an in-line script, the only difference is that you specify src attribute in the <script> tag to call JavaScript code from an external text file and there's no actual code between <script> and </script>.


<script type="text/javascript" src="filename.js"> </script>
Example :
js demo

Keep in mind, <script> tag should not be used in a external JavaScript file. In the HTML file, you can use <script> tag to indicate the location of a JavaScript file.


js demo


Output:
75

Similarly, you can display a warning message to the user by using <noscript> tags. It defines an alternate content for users who have disabled scripts in their browser or have a browser that doesn't support script. It can be used in both <head> and <body>. For that, add a noscript block immediately after the script block. So the content inside the <noscript> element will be displayed if scripts are not supported, or are disabled in the user's browser.


Example :
js demo

Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

Total Visits
Traffic feed status