-->

Monday, April 6, 2015

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.
© Copyright 2013 Computer Programming | All Right Reserved