Javascript Basics

Hi bloggers..,

Today we will see the javascript basics. :). Even JS supports for windows8 apps too 😉

Learn basics of javascript in w3schools

x=document.getElementById(“demo”)  //Find the element
x.innerHTML=”Hello JavaScript”;    //Change the content
The DOM (Document Object Model) is the official W3C standard for accessing HTML elements.
Onclick of an image..you can change the image also..check the following code:

<html>
<body>
<script>
function changeImage()
{
element=document.getElementById(‘myimage’)
if (element.src.match(“bulbon”))
{
element.src=”pic_bulboff.gif”;
}
else
{
element.src=”pic_bulbon.gif”;
}
}
</script>

<img id=”myimage” onclick=”changeImage()”
src=”pic_bulboff.gif” width=”100″ height=”180″>

<p>Click the light bulb to turn on/off the light</p>

</body>

</html>

To find input text is number or not..use isNaN(x) in js.

The isNaN() function determines whether a value is an illegal number (Not-a-Number).

This function returns true if the value is NaN, and false if not.

isNaN(value)

Variable Declaration:

var x=5;
var y=6;
var z=x+y; //is very similar to normal algebra

JavaScript Data Types

JavaScript variables can also hold other types of data, like text values (name=”John Doe”).

In JavaScript a text like “John Doe” is called a string.

There are many types of JavaScript variables, but for now, just think of numbers and strings.

When you assign a text value to a variable, put double or single quotes around the value.

When you assign a numeric value to a variable, do not put quotes around the value. If you put quotes around a numeric value, it will be treated as text.

Example

var pi=3.14;
var name=”John Doe”;
var answer=’Yes I am!’;
var x                // Now x is undefined
var x = 5;           // Now x is a Number
var x = “John”;      // Now x is a String

Objects:

“Everything” in JavaScript is an Object: a String, a Number, an Array, a Date….

In JavaScript, an object is data, with properties and methods.

Properties and Methods

Properties are values associated with an object.

Methods are actions that can be performed on objects.


A Real Life Object. A Car:

Properties:

car.name=Fiat

car.model=500

car.weight=850kg

car.color=white

Methods:

car.start()

car.drive()

car.brake()

The properties of the car include name, model, weight, color, etc.

All cars have these properties, but the values of those properties differ from car to car.

The methods of the car could be start(), drive(), brake(), etc.

All cars have these methods, but they are performed at different times.

Objects Usage:

You can use objects in the following way..

<html>
<body>

<script>
var person=new Object();
person.firstname=”John”;
person.lastname=”Doe”;
person.age=50;
person.eyecolor=”blue”;
document.write(person.firstname + ” is ” + person.age + ” years old.”);
</script>

</body>
</html>

Learn Javascript from the following websites:

http://www.learn-javascript-tutorial.com/javascript-basics.cfm

http://www.w3schools.com/js/DEFAULT.asp

Advertisements