-->

Thursday, February 4, 2016

How to use JavaScript in Master Page ContentPlace Holder in ASP.NET

In this article, I will explain you, How to use JavaScript code in ContentPlaceHolder of Master page. If your web page (Web Form=.aspx page)  is inherit from master page then you must to use ClientID of the control to get the value. Like

document.getElementById("<%=Label1.ClientID%>");

By using above mentioned code we want to retrieve inner html text of the label. If you are using java script in simple web form which is not inherit from master page then you can use simple id of the control as parameter. Like

document.getElementById("Label1");

How to use JavaScript in Master page's Content PlaceHolder

Step-1 :  Add a Master page also add web form.
Step-2 :  Do Inherit web form from master page.
Step-3 :  Write the below mentioned code in the content place holder of the web form.

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <input id="Button1" type="button" value="button" onclick = "check()"/>
    <script type = "text/javascript">
        function check()
        {
             var label = document.getElementById("<%=Label1.ClientID%>");
             var textbox = document.getElementById("<%=TextBox1.ClientID%>");
             alert("Label Value " + label.innerHTML + "TextBox Value  " + textbox.value);
           
        }
    </script>
</asp:Content>

Note: Please mentioned javascript code at last position of the page.


Friday, June 19, 2015

Print the asp.net webpage using button

Introduction

Today i am talking about printing, and how to print the content or you can say selected content in asp.net. If you have a webpage and you want to print this then you have to choose command key (ctrl+p) for that. Also you want to print the selected part then you have to select the text first then you have to use command key. But sometimes your selected page could not printed. So many websites provide the printing facilities on the webpage. Today i am talking about the same topics here.
Lets take a simple example to demonstrate the topic.

Source code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="printpage.aspx.cs" Inherits="printpage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
<script>
function printpage() {

var getpanel = document.getElementById("<%= Panel1.ClientID%>");
var MainWindow = window.open('', '', 'height=500,width=800');
MainWindow.document.write('<html><head><title>Print Page</title>');
MainWindow.document.write('</head><body>');
MainWindow.document.write(getpanel.innerHTML);
MainWindow.document.write('</body></html>');
MainWindow.document.close();
setTimeout(function () {
MainWindow.print();
}, 500);
return false;

}
</script>
</head>
<body>
    <form id="form1" runat="server">
<asp:Panel ID="Panel1" runat="server">

<table style="width: 100%;">
<tr>
<td>English Marks</td>
<td>Social Marks </td>
<td>Science Marks</td>
</tr>
<tr>
<td>60</td>
<td>56</td>
<td>23</td>
</tr>
<tr>
<td>56</td>
<td>12</td>
<td>23</td>
</tr>
</table>
</asp:Panel>
    <asp:Button ID="Button1" runat="server" OnClientClick="return printpage();"  Text="Print Page" />
    </form>
</body>
</html>
Code generate the following output:


Print the asp.net webpage using button

In this example i have a panel control with table. Table contains data. Also i have a button control in the page . When we click on it, a javascript function will raised. In this function, first of all get the panel with their id property. Create a window with the help of window.open( ) method also set the width and height of it. Now, write the html code in the window by using write method also write the panel contents in the body section. After that print the window.

Monday, April 6, 2015

Collapse or Expand slide using Java Script

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
div#box1
{
    background-color : Gray;
    height:200px;
    width : 200px;
    overflow : hidden ;
}


</style>
<script type ="text/javascript">
    function slideopen(ele) {
        var elem = document.getElementById(ele);
        elem.style.transition = "height 0.2s linear 0s";
        elem.style.height = "200px";
    }
    function slideclose(ele) {
        var elem = document.getElementById(ele);
        elem.style.transition = "height 0.2s linear 0s";
        elem.style.height = "0px";
    }

</script>

    <title>Change Background color</title>
</head>
<body>
<button onclick="slideopen('box1');"> slideopen</button>
<button onclick="slideclose('box1');"> slideclose</button>

<div id="box1">Paste your content here</div>
</body>
</html>

Code generate the following 

Collapse or Expand slide using Java Script

Collapse or Expand slide using Java Script

Element fadeout, FadeIn using Java Script

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
div#box1
{
    background-color : Gray;
    height:200px;
    width : 200px;
}


</style>
<script type ="text/javascript">
    function fadeout(ele) {
        var elem = document.getElementById(ele);
        elem.style.transition = "opacity 1.0s linear 0s";
        elem.style.opacity = 0;
    }
    function fadein(ele) {
        var elem = document.getElementById(ele);
        elem.style.transition = "opacity 1.0s linear 0s";
        elem.style.opacity = 1;
    }

</script>

    <title>Change Background color</title>
</head>
<body>
<button onclick="fadeout('box1');"> fadeout</button>
<button onclick="fadein('box1');"> fadein</button>

<div id="box1">Paste your content here</div>
</body>
</html>

Code generate the following output


Element fadeout, FadeIn using Java ScriptElement fadeout, FadeIn using Java Script


Example of Navigator object in Java Script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

    <title>Navigator objects</title>
</head>
<body>
<script type="text/javascript" >
    document.write("User Agent Header " + navigator.userAgent +"<br/>");
    document.write("Browser Code name: " + navigator.appCodeName + "<br/>");
    document.write("Browser name: " + navigator.appName + "<br/>");
    document.write("Browser Version: " + navigator.appVersion + "<br/>");
    document.write("Cookies Enabled " + navigator.cookieEnabled + "<br/>");
    document.write("Platform " + navigator.platform + "<br/>");

</script>
</body>
</html>

Code generate the following Output

Example of Navigator object in Java Script


Required validation in Java Script

<!DOCTYPE html>

<html>
<head>
    <title>encode and decode</title>
    <script type="text/javascript" >
        function validation() {
            var val = document.getElementById('in');
            if (val.value.length == 0) {
                alert("Required");

            }
        }
        
    </script>
</head>
<body>
<h1>Example of required field validation control</h1>
<input type="text" name="gt" value="" id="in" />
<button onclick="validation();">perform validation</button>
</body>
</html>

Code generate the following output

Required validation in Java Script

Compare validation using Java Script

<!DOCTYPE html>
<html>
<head>
    <title>encode and decode</title>
    <script type="text/javascript" >
        function validation() {

            var firstvalue = document.getElementById('first');
             var secondvalue = document.getElementById('second');
             if (firstvalue.value != secondvalue.value) {
                alert("Password not match");

            }

        }
        
    </script>
</head>
<body>
<h1>Example of compare field validation control</h1>

Password<input type="password" name="gt" value="" id="first" /><br />
Re-type<input type="password" name="gt" value="" id="second" />
<button onclick="validation();">perform validation</button>

</body>
</html>

Code generate the following output

Compare validation in Java Script

How to add TextBox value in Java Script

<!DOCTYPE html>

<html>
<head>
    <title>encode and decode</title>
    <script type="text/javascript" >

        function validation() {

            var firstvalue = document.getElementById('first');
            var secondvalue = document.getElementById('second');
            var finalvalue = eval((firstvalue.value)+ "+" +( secondvalue.value));
            alert(finalvalue);

        }
        
    </script>
</head>
<body>
<h1>Addition of two or more number</h1>

Enter Number<input type="text" name="gt" value="" id="first" /><br />
Enter Second<input type="text" name="gt" value="" id="second" />
<button onclick="validation();">Add number</button>

</body>
</html>

Code generate the following output

How to add TextBox value in Java Script

Example of Count Down timer in Java Script

<!DOCTYPE html>

<html>
<head>
    <title>encode and decode</title>
    <script type="text/javascript" >
        function Timercount(second,outwin) {
         
            var stat = document.getElementById(outwin);
            stat.innerHTML = "Please wait for " + second + "seconds";
            if (second < 1) {
                clearTimeout(timervalue);
                stat.innerHTML = '<h3> CountDown Complete</h3>';
            }
            second--;
            var timervalue = setTimeout('Timercount(' + second + ',"' + outwin + '")', 1000);

        }
     
    </script>
</head>
<body>
<div id="first"></div>
<script type="text/javascript">Timercount(10,"first");
</script>

</body>
</html>

Code Generate the following output

Example of Count Down timer in Java Script
Example of Count Down timer in Java Script

How to change background-color of element using Java Script

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
div#box1
{
    background-color : Gray;
    height:200px;
    width : 200px;
}


</style>
<script type ="text/javascript">
    function backcolo(ele, clr) {

        var elem = document.getElementById(ele);
        elem.style.transition = "background 1.0s linear 0s";
        elem.style.background = clr;

    }

</script>

    <title>Change Background color</title>
</head>
<body>
<button onclick="backcolo('box1','Red');"> Red Color</button>

<button onclick="backcolo('box1','green');"> Green Color</button>

<button onclick="backcolo('box1','orange');"> orange Color</button>

<div id="box1">Paste your content here</div>
</body>
</html>

Code generate the following code

How to change background-color of element using Java Script How to change background-color of element using Java Script
How to change background-color of element using Java Script change background color of element

Object properties and Methods in Java Script

These objects all have particular properties and methods that are associated with their respective objects and they can be accessed and manipulated. A property of an object is a predefined variable that you can assign a value to with simple dot notation syntax like this:

objectNamePropertyName = value

For instance, if you want to change background color of document object to yellow, you would access the bgColor property and assign the String “yellow” to it like this:

Document.bgcolor=”yellow”

A Method of an Object is just a predefined function that has already been assigned to an Object by Netscape. You invoke a Method on an Object with same dot notation like this.

objectName.methodName( )

Window Object is the parent object of all other objects In a page, which means it’s at the top of the hierarchy of all page content, including those that have FRAMESETs and FRAMEs. All other objects are its descendants or child objects. Some of the child objects like the document object are also parent or container objects for other objects that are lower down in the hierarchy. All child objects are said to be properties of their respective parent objects and such they can be manipulated and referenced with java script.
Suppose you want to give focus to window at some point in script. You do that by referencing the window object and calling the focus( ) method like this

Window.focus( );

Window object is always the parent object, Netscape allows you to omit it from expression that invoke other objects, since it is always assumed to be there.

Function in Java Script

A function is a process that incorporates a sequence of java script statement to accomplish a task. To integrate a function into document you have to first define it within a SCRIPT, which is contained in the in the head element. Then you have to call function. Calling the function can be accomplished in several ways, including the ability to call a function from within another function. In many instances function is called as the value of an event handler of an HTML element, which is called attribute assignment. The simplest way to call a function is to use it by name as statement. You can use the return statement to return a value with a function. It’s usually a good idea to define functions in the head of document so that functions are loaded first, before any of the HTML elements in the body have loaded. This precludes the generation of many java script runtime errors due to a script in one part of document trying to interact with another script that hasn’t loaded yet. This occurs when the user tries to click a button object before the page fully loads, and button needs that unloaded code before it will work. Since that required code hasn’t loaded yet, you get a runtime error. There are exceptions to HEAD Scripts.

Defining a function with function statement

Defining a function starts by using function keywords, which is followed by the name of function and then a comma-separated list of arguments that are enclosed within parentheses. You can define a function that takes no arguments, but you still have to include the parentheses. Next comes a sequence of Statements that are semicolon separated and enclosed within curly braces {}. The following function setColor() takes no Argument and sets the background color to blue when it is called.

Function setColor()
{
document.bgcolor=”blue”;

Syntax:

Function functionName(argument1, argument 2,………,argument)
{
Statement 1;
Statement 2;
}

Calling a function by attribute assignment

Defining a function is the first step in getting a function to execute its Statements. Next you have to call function within a SCRIIPT or you have to assign it to an event handler of an html element, which is shown in the following example:
To start the process for this scenario you could code previously defined setColor() function so that it was more useful by adding a color Argument to it like this.

Function setColor(color)
{
Document.bgcolor=color;
}

So that when setColor() function is called, background will be changed to color argument that is provided at that time, which can be different each time function is called. For instance, suppose you define three button Objects that each have setColor() function assigned to its respective onClick Event Handler, like this:

<input type=”button” value=”red it” onClick=”setColor(‘red’);”>
<input type=”button” value=”blue it” onClick=”setColor(‘blue’);”>
<input type=”button” value=”green it” onClick=”setColor(‘green’);”>

Then, if the user clicks on the first button, setColor() function uses String ‘red’ as the color argument to change the background color to red. If the user clicks on the second Button, the onClick Event Handler uses the same setColor() function but has a different String value to provide as the color argument so that the background color gets changed to blue. The same is true for the button with ‘green’ as the color argument.

Example of inline scripts in java script

When scripts are executed from top to bottom, means your script appear in both <head> as well as <body> section. No one scripts is executed inside the tag known as inline scripts. Let’s take a simple example.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" >
    alert("Hello");

</script>
    <title></title>
</head>
<body>
<script type="text/javascript" >
    alert("World!");

</script>
</body>
</html>

Code Generate the following output

Example of inline scripts in java script

Example of inline scripts in java script

How to print date in Java Script

Date object enables Java Script programmers to create an object that contains information about a particular date and provides a set of methods to work with that information. To create an instance of the date object, use the keyword new as follows:
var my_date = new Date(parameters);
The parameter, if left empty, indicates today's date & time . The parameter could be any specific date format.

Date object provides the following methods.

getDate( ) : Returns date of the month as an integer from 1 to 31.
setDate( ) : Sets date of the month based on an integer argument from 1 to 31.
getHours( ) : Returns hours as an integer from 0 to 23.
setHours( ) : Sets hours based on an argument from 0 to 23.
getTime( ) : Returns number of milliseconds since 1 feb 2014 at 00:00:00
setTime( ) : Sets time based on an argument representing the number of Milliseconds since 1 feb 2014 at 00:00:00.

Date class gives the current date. You can take Date class object in a single variable like

Var today= new Date();


This object represent the current date. You know very well that every line of code in java script is known as statement. Lets take an simple example

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
<script type ="text/javascript" >
    var todaydate = new Date();
    document.write("Date is" + todaydate.toString());

</script>
</body>
</html>

Code generate the following output
How to print date in Java Script

Operators and Expressions in Java Script

An operator is used to transform one or more values into a single resultant value. The values to which the operator is applied is referred to as operands. A combination of an operator and its operands is referred to as an expression . This is the value that results when an operator is applied to its operands.
1. Arithmetic Operators
Arithmetic operators are the familiar operators because they are used every day to solve common math calculations. The arithmetic operators that JavaScript supports are:

Operator
Description
+
Addition
-
Subtraction or Unary negation
*
Multiplication
/
Division
%
Modulus
++
Returns the value then Increment
--
Return the value then Decrement

2. Logical Operators:
Logical operators are used to perform Boolean operators on Boolean operands and, or, not. The logical operators supported by Java Script are:
Operator
Description
||
Logical Or
&&
Logical and
!
Logical not

3. The comparison operators
Comparison operators are used to compare two values, supported by JavaScript are:
Operator
Description
==
Equal
===
Strictly Equal
!=
Not Equal
!==
Strictly Not Equal
< 
Less than
<=
Less than or equal to
> 
Greater than
>=
Greater than or equal to

4. String Operators
String operators are those operators that are used to perform operations on strings JavaScript supports only the string concatenation (+) operator.
This operator is used to join two string. Eg. "xy" + "cd" produce "xycd".

5. Assignment Operator
Assignment operator is used to update the value of a variable. Few assignment operators are combined with other operators to perform a computation on the value contained in a variable and then update the variable with the new value.

6. Conditional Expression Ternary Operator 
JavaScript supports conditional expression operator. They are ? and : the conditional expression operator is a ternary operator since it take three operands, a condition to be evaluated and two alternative values to be returned based on the truth or falsity of the condition.
Syntax

condition ? value 1: value ?

The condition expressed must return a value true or false. If the condition is true, value 1 is the result of the expression, otherwise value 2 is the result of the expression.

7. Special Operators
Java Script supports a number of special operators that do not fit into operator categories covered above.
1. delete Operators:
The delete operator is used to delete a property of an element or an object at an array index.
Example

delete test[6]

Deletes the seventh element of test.
2. new operator:
new operator is used to create an instance of an object type.
Example 

test = new Array()

This will create a new JavaScript object of the type array and assign this array to a context area in memory called test.
3. void operator:
void operator does not return a value. It is typically used in Java Script to return a URL with no value.

Data Types and Literal in JavaScript

JavaScript support four primitive types of values and supports complex types such as objects and arrays. Primitive type are types that can be assigned a single literal value such as number, string or boolean value. Literal are fixed values, which literally provide a value in a program.

The Primitive data type are:

1. Number :
Consists of integer and floating point numbers and a special NaN (not a number) value.
Integer Literals can be represented in JavaScript in decimal, hexadecimal, and octal form .
Floating-point literals are used to represent numbers that require the use of a decimal point, or very large or very small numbers that must be written using exponential notation. A floating-point number must consist of either a number containing a decimal point or an integer followed by an exponent.
Eg : 22, 15.20, -36.7, 2E5, 0x6F

2. Boolean :
Java Script supports a pure boolean type that consists of two values true and false.
Logical operators can be used in Boolean expressions.
Java Script converts the boolean values true and false into 1 and 0 when they are used in numerical expressions.

3. String :
consists of string values that are enclosed in double or single quotes.
Java Script provides built-in support for strings. A string is a sequence of Zero or more characters that are enclosed by double(") or single (') quotes. If a string begins with a double it must end-with a double quote. If a string begins with a single quote it must end with a single quote.
Eg : "james", '2, USA'

4. Null :
Null value is common to all JavaScript types. It is used to set a variable to an initial value that is different from other valid values. Use of the null value prevents the sort of errors that results from using uninitialized variables. Null value is automatically converted to default values of other types when used in an expression.

5. Type Casting :
In Java Script variables are loosely cast. The type of a Java Script variable is implicitly based on the literal values that are assigned to it from time to time.
For instance, combining the string literal " Total amount is " with the integer literal 1000 results in a string with the value "total amount is 1000" . Adding together the literal 10.5 and the string "20" results in the floating point integer literal 30.5. This process is known as type casting.

Casting Variables
In other to make working with data types convenient, variables are created . In Java Script variables can be created that can hold any type of data.
Declaring a variable tells JavaScript that a variable of a given name exists so that the Java Script interpreter can understand references to that variable name throughout the rest of the script.
Although it is possible to declare variables by simply using them, declaring variables helps to ensure that programs are well organized and helps keep track of the scope of the variables. Variables can be declared using var command.
Syntax

var <variable name:> = value ;

Example :
var first_name;
var last_name="jacob";

The equal sign (=) used in assigning a value to variable is known as an assignment operator.
92J5UZPS8GTE

Java Script Advantages

  1. Embedded within HTML  : Java Script does not require any separate editor for programs to be written, edited or compiled. It can be written in any text editor like Notepad, along with appropriate HTML tag and saved as filename.html. HTML files with embedded Java Script commands can then be read and interpreted by any browser that is Java Script enabled. 
  2. An Interpret Language : Java Script is an interpreted language i.e, no compilation is needed. The syntax is completely interpreted by a browser just as it interprets HTML tags. This provides an easy development process.
  3. Minimal Syntax - Easy to Learn : By learning few commands and simple rules of syntax, complete applications can be built using Java Script.
  4. Designed for Simple, small Programs : It is well suited to implement simple, small programs( for example, a program to apply all arithmetic operation on two numbers). Such programs can be easily written and executed using Java Script. They can be easily integrated into a web page.
  5. Quick Development : Java Script does not require time-consuming compilation, so scripts can be developed in a short period of time. This is the reason that many GUI interface features such as alerts, confirm boxes, prompts, and other GUI elements, are handled by client side Java Script, the browser and HTML code.
  6. Procedural Capabilities : Every programming language needs to support facilities such as condition checking, Branching and looping Java Script provides syntax, which can be used to add such procedural capabilities to web page coding.
  7. Performance : Java Script can be written such that the HTML files are fairly compact and quite small. This minimizes storage requirements on a web server and download time for the client.
  8. Event Handling : Java Script supports object or Event based programming. Java Script recognizes when a form 'Button' is pressed. The event can have suitable JavaScript code attached, which will execute when the 'Button Pressed' event occurs. 
  9. Event handling : Java Script can be used to implement context sensitive help. Whenever in a HTML form's mouse cursor moves over a button or a link on the page, a helpful and informative message can be displayed in the status bar at the bottom of browser window.
  10. Platform Independence and Architecture Neutral :  Java Script is a programming language that is completely independent of the hardware on which it works . It is a Language that is understood by any Java Script enabled web browser. Thus, Java Script application works on any machine that has an appropriate Java Script enabled web browser.
  11. A Java Script program  developed on a window machine will work perfectly well on a Unix machine.
  12. A platform specific browser, maintained at client end, does the interpretation of Java Script, relieves the developer from the responsibility of maintaining multiple source code files for multiple platform.
  13. Easy Testing and Debugging : Being an interpreted language, Java Script scripts are tested line by line, and errors are also listed as they are encountered, i.e. an appropriate error message along with line number is listed for every error that is encountered. It is thus easy to locate errors, make changes, and test it again without the overhead and delay of compiling.

Sunday, April 5, 2015

Functions Provided by Java Script

  1. Java Script can put dynamic text into an HTML page- using HTML, you can produce only static pages but javascript add dynamic functionality in HTML. A javascript statement like this: document.write("<h1>"+name+"</h1>") can write a variable text into an HTML page.
  2. JavaScript gives HTML designers a programming tool - HTML authors are normally not programmers, but javascript is a scripting language with a very simple syntax. Almost anyone can put small "snippets" of code into their HTML page.
  3. JavaScript can be used to Validate data -  A JavaScript can be used to validate form data before it is submitted to a server. This saves the server from extra processing.
  4. JavaScript can react to events -  Java script provide the concepts of events handling. A JavaScript can be set to execute when something happens like when a page has finished loading or when a user clicks on an HTML element. Eg button.
  5. Java Script can read and write HTML elements- A JavaScript can read and change the content of an HTML element.
  6. Java Script can be used to detect the visitor's browser - A Java Script can be used to detect the visitor's browser, and - depending on the browser-load another page specifically designed for the browser.
  7. Java Script can be used to create cookies - A Java Script can be used to store and retrieve information on a visitor's computer. 

Introduction to Java Script

Java Script is used in many of Web pages to improve the design, validate forms and create cookies.
JavaScript is the popular scripting language on the internet, and works in all major browser, such as Internet Explorer, Opera and Firefox.
JavaScript, despite the name, is essentially not related to Java programming language, both have the common C syntax, and JavaScript copies many Java names and naming conversions. The language was initially named "LiveScript" but was renamed in a co-marketing deal between Sun and Netscape in exchange for Netscape bundling Sun's Java runtime with their dominant browser. key design principles within JavaScript are inherited from the Self and Scheme programming languages.

History

JavaScript was developed by Brendan Eich of Netscape under the name Mocha, which was later renamed to 'LiveScript', and finallt to JavaScript. Change of name from LiveScript to JavaScript roughly coincided with Netscape adding support for Java technology in its Netscape navigator web browser. JavaScript was first introduced in the Netscape browser version 2.0B3 in December 1995. Naming was caused confusion, giving the impression that language is a spin -off  of Java, and it has been characterized by many as a marketing ploy by Netscape to give JavaScript the cachet of what was then the hot new web -programming language.
Microsoft named its dialect of language JScript to avoid trademark issues. JScript was first supported in Internet Explorer version 3.0 released in August 1996, and it included Y2K-compliant date functions, unlike those based on java.util.Date in JavaScript at the time. Dialects are perceived to be so similar that the terms "JavaScript" and "JScript" are often used interchangeably Microsoft notes dozens of ways in which JScript is not ECMA complaint.
Netscape submitted JavaScript to ECMA International for standardization resulting in standardized version named ECMAScript.

JavaScript

  1. Java Script is a scripting language.
  2. Java Script was designed to add interactivity to HTML pages.
  3. A Scripting language is a lightweight programming language.
  4. Java Script is an interpreted language (means that scripts execute without preliminary compilation)
  5. Java Script is usually embedded directly into HTML pages.
  6. Everyone can use Java Script without purchasing a licence.

Writing Javascript into HTML

JavaScript syntax is embedded into HTML file. A Browser reads HTML files and interprets HTML tags. Since all JavaScripts need to be included as an integral part of an HTML document when required, the browser needs to be informed that specific sections of HTML code is JavaScript. The Browser will then use its built-in JavaScript engine to interpret this code.
The Browser is given this information using HTML tags <SCRIPT>....</SCRIPT>.  The <SCRIPT> tag marks the beginning of a snippet of scripting code. The paired </SCRIPT> marks the end of a snippet of script code.
The <SCRIPT> tag takes an optional attribute, as listed below:

Attributes
Description
Language
Indicates the scripting language used for writing the snippet scripting code. It can take following values:
        1JavaScript
     2. VBScript

Syntax:

<script language="javascript">
<!-- JavaScript statements 
// -->
</script>

For example

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
<script type ="text/javascript" >
    document.write("Hello World!");

</script>
</body>
</html>

The code above will produce this output on an HTML page: 


Writing Javascript into HTML

To insert a JavaScript into an HTML page, we use the <script> tag. Inside the <script> tag we use the type attribute to define scripting language.
So, the <script type="text/javascript"> and </script> tells where a JavaScript starts and ends.
The word document.write is a standard JavaScript command for writing output to a page. 
By entering the document.write command between the <script> and </script> tags. the browser will recognize it as a JavaScript command and execute the code line. In this case the browser will write Hello World! to the page.
© Copyright 2013 Computer Programming | All Right Reserved