WEB TECHNOLOGY
Javascript in terms of HTML
Todays post is concerned about how javascript is embeded in html page in order to make our page look dynamic and for additional we may beautify our page by applying various css properties.
Javascript and Java, People often confuse about this two languages javascript is object based where as java is object oriented but both share same properties almost just as cpp and java both have more or less same concept if one knows cpp one would get command on java same is the case with javascript.
JAVASCRIPT
- It is Object based
- It may be client side scripting language or server side scripting lamguage.
Here in this post we will cover up some points on client side
- In client side the code is visible.
- Code can be modified.
- Local files and Database cant be accessed as they are stored in server.
- It has less features as compared to serverside language.
- Example are vb script and javascript.
Embedding javascript in HTML
- Java script is embedded in HTML document by the script tag
- whatever content you want to write in java script just write within script tag
SYNTAX
<script type="text/javascript">
</script>
Let us go through a simple example:-
BY INTERNAL SCRIPT
<html>
<head>
</head>
<body>
<script>
document.write("hello world");
</script>
</body>
</html>
The above example prints hello world and method is of javascript document.write("");
BY EXTERNAL SCRIPT
Write the code of java script in seperate file and we will be able to embed this file in multiple pages just by linking the file in the particular page. Save the file by extension .js before you link in any HTML file.
file 1 // save as 1.js
<script>
document.write("hello world");
</script>
file 2// Html file
<html>
<head>
</head>
<body>
<script src="1.js"></script>
</body>
</html>
By external javascript we can save the labour of writing each and every time if we want to impart the same code in more than one file
Again going further in java script............
- Javascript is embeded in webpages and interpreted by web browser.
Variable in java script
In javascript Variable is defined by var statement.
for example var a or var a , b , c;
Scope is either local or global or blocked scope as in c and cpp.Butif not declared with var statement the scope is global.
Objects in java script
An object is the collection of values or function that it represents that is it defines some code attached to it.
In javascript Variable is dfined by var statement.
For objects in Javascript we have two types.
- Built in objects
- User defined objects
Built in object means the objects which are actually previously defined and present for example we take Math.PI will calculate PI for given number.Where as for userdefined we define the object ourselves.
Before we go to the Syntax of object we would make our concepts clear that A property OR a Function is attached with the object.
object is declared as :- var myobj=new object();
myobj.propertyname OR myobj.methodname(Parameters)
To initialize the values:-
var person =new Object()
person.firstname="kushal";
person.lastname="keval";
person.age=32;
Now if we want a property that is persons hair than
person.hair =new Object(); //creates a property hair associated with person
person.hair.Length="long";
person.hair.color="red";
ARRAYS IN JAVASCRIPT
Array is the collection of data.Each item in the array has an index to access it with they begin with 0, So first item will be stored at zeroth index.
Arrays are created using the array constructor ..........
var myarray = new Array();
a[0]=1;
a[1]=2;
a[3]="three";
a[4]=false;
So it is not necessary that it is used to store the same datatype in javascript an array can store elements belonging to multiple datatypes. so it can be included in loosely typed language .
For storing initial values with the help of constructor
var myarray = new myarray(1,2,"three",false);
One can declare the number of elements it can have
var myarray = new myarray(15);
Comments
Post a Comment