Beruflich Dokumente
Kultur Dokumente
Where To Place JavaScript :You can place in <body> or the <head> sections in HTML page or also
placed in external files with .js extension
Note: <script type="text/javascript"> The type attribute is not required. as JavaScript is the default
scripting language in HTML.
Dom Hierarchy
Arrays: Used to store multiple values in a single variable (Multiple values of the same type (such as
strings,Int).Index start with 0
Note: In JavaScript Arrays are special kinds of objects. So you can have different types of values or diff
objects or functions in an Array.
Methods:
Changing the value of an array item: var myArray = [ 'hello', 'world' ]; myArray[1] = 'changed';
a = new Date(); // Creates a new date object and assigns the current time to it
document.write(a.getTime()); : Returns a millisecond representation of a date. ex.
1270487133410
document.write(a.getDay()); : Returns the day of the week
document.write(a.getFullYear()); : Returns the year of the date
document.write(a.getHours()); : Returns the hours of the set date
document.write(a.getMinutes()); : Returns the minutes of the set date
Function expression :
Along with parameters , arguments object also get passed to a JavaScript function.
what parameters, how many parameters passed can be find using arguments.
Value of parameters can be read using the argument indexes.
function ShowTotal() { var s = 0;
for (var i = 0; i < arguments.length;i++){ s+=arguments[i];}
return s;
} document.write(ShowTotal(10,20,30)); </script>
) (10,20);
3. var r= (function(a,b)
(20,5);
alert(r.toString());
Object as literal: Name-value pairs(separated with :) and list with comma-separated ecclosed in curly
braces
var myObject = {
sProp: 'some string value',
numProp: 2,
bProp: false
};
Note:Object literal property values can be of any data type, including arrays, functions, and nested
object
var Swapper = {
// an array literal
images: ["smile.gif", "grim.gif", "frown.gif", "bomb.gif"],
pos: { // nested object literal
x: 40,
y: 300
},
onSwap: function() { // function
// code here
}
};
}
console.log(fooarr[3].val);
this.area = function () {
return this.height * this.width;
};
};
Object Prototypes
All the objects such as functions in JavaScript contain a prototype object. When we use function as
constructor to create object, properties of prototype object get available to the newly created objects.
We can solve the above problem of area function getting redefined using the prototype object of the
constructor.
function Rectangle(height, width) {
this.height = height;
this.width = width; }
Rectangle.prototype.area = function () {
return this.height * this.width; };
var rec1 = new Rectangle(45, 6); var rec2 = new Rectangle(8, 7);
In above listing we are creating the area function as the property of the Rectangle prototype. Hence it
will be available to all new objects without getting redefined.
Keep in mind that every JavaScript object has a second object associated with it called prototype object.
Always the first objects inherits the properties of the prototype object.
function emp(name){ this.Name=name;
this.getName=function(){
return this.name; } }
var e1=new emp('Mark');
var e2=new emp('shaik');
Problem:if you create 100 emp objects there will be 100 copies of getName() function.
Instead we want all the objects share same function code.
function emp(name){ this.Name=name; }
emp.getName=function(){
return this.name; }}
Advantages:1.No matter how many objects you create,functions are loaded only once into memory.
function emp(name){}
emp.prototype.getname=function(){ return this.name; }
var e1=new emp('Mark');
Object.create()
The Object.create() static method was introduced in ECMA Script 5.0. It is used to construct new object.,
var foo = Object.create(Object.prototype,
console.log(foo.name);
console.log(koo.name);
console.log(koo.subject);
In the above listing, we have an object named foo, and we’re using foo as the prototype of the object
named koo. Koo will inherit the properties of foo and it will have its own additional properties also.
Accessing Object Properties;
objectName.propertyName or objectName["propertyName"]
indexOf() method returns the index of first occurrence of a specified text in a string:
The lastIndexOf() method returns the index of the last occurrence of a specified text in a string:
Both the indexOf(), and the lastIndexOf() methods return -1 if the text is not found.
slice(start, end)
substring(start, end)
substr(start, length)
substring() is similar to slice().The difference is that substring() cannot accept negative indexes.
Example
var str = "Apple, Banana, Kiwi";
var res = str.substring(7,13); //Banana
Note:If you know the indices, use slice(); if you know the length, use substr().
Substr() Method:substr() is similar to slice().The difference is that the second parameter specifies the
length of the extracted part.
Note: When you place runat="server" in an standard HTML tag,to get the html element in javascript use
function GetHiddenvalue() {
var txt_value=document.getElementByid('<%=textbox1.ClientID%>').value;
scriptmanager.registerstartupscript(page,typeof(page),"message","javascript:alert-msg(function
name);",true);
//code behind
scriptmanager.registerstartupscript(page,typeof(page),"message","javascript:getvalue('"
+txt_name.ClientID +"') ",true);
var str=setInterval(function(){start_timer()},3000);
var str=document.getElementByid('<%=ddldropdownlist1.ClientID%>')
var text=str.options[str.selectedindex].text
<script type="text/javascript">
function validate() {
function isvalidusername() {
var uid;
var temp = document.getElementById('<%=txtfname.ClientID%>');
uid = temp.value;
if (uid == '') { return ("please enter firstname" + "\n"); }
else { return '';}
}
function isvalidFirstname() {
var firstname;
var temp = document.getElementById('<%=txtuser.ClientID%>');
firstname = temp.value;
if (firstname == '') { return ("please enter username" + "\n"); }
else { return ''; }
}
function isvalidlocationname() {
var locname;
var temp = document.getElementById('<%=txtlocation.ClientID%>');
locname = temp.value;
if (locname == '') { return ("please enter location" + "\n"); }
else { return ''; }
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>UserName</td>
<td><asp:TextBox ID="txtuser" runat="server" /></td>
</tr>
<tr>
<td>First Name</td>
<td><asp:TextBox ID="txtfname" runat="server" /></td>
</tr>
<tr>
<td>Location</td>
<td><asp:TextBox ID="txtlocation" runat="server" /></td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnsave" runat="server" Text="Save"
OnClientClick="javascript:validate()" />
</td>
</tr>
</table>
</form>
</body>
</html>