Posted by Unknown on 12:59 AM
Labels:

JavaScript is a scripting language widely used for client-side web development.
JavaScript, despite the name, is essentially unrelated to the Java programming language,
although both have the common C syntax, and JavaScript copies many Java names and naming conventions.
The language's name is the result of a co-marketing deal between Netscape and Sun.


JAvascript is embedded within html.

A simple Hello World Example:

<html>
<head>
<title>HEllo World using Javascript</title>
<script language="JavaScript">
document.write<"Hello World!">;
</script>
</head>
<body>
...body code goes here...
</body>
</html>

We can include the below for browsers tat wont support javascript

<noscrip>
.....
</noscript>

Above we see that the script is included within the (head)(/head) tags.But we can include the script within the (body) also.

<body>
<script language="JavaScript">
!--
document.write("Hello World");
// --)
</script>

We can also include an external javascript file for our page

<script language="JavaScript" src="hello.js"></script>

above we have seen that to output text we use the write() or writeln() methods of the document object..

document.write("Hello World");

Document object can also be used to change the background color or foreground color.

document.bgColor="black"
document.fgColor="white"


We all have noticed messageboxes on webpages..Javascript has basically three types of messageboxes:

alert
confirm
prompt

window.alert("Invalid!")

window.confirm("Are you sure you want to quit?")

window.prompt("Enter ur username here")

We can also write it as alert(),confirm(),or prompt()

Variable declaration in JavaScript is done using var

var x=window.prompt("Enter your username");
window.alert(x);

Functions are used to include chunks of code and call the javascript code on events.

<script>
function ss<>
{
alert<"Thank you!">
}
</script>

Events are triggers that are executed when something like click,load etc happens..

OnClick

<script>
function btnClick<>
{
alert<"Thanks">
}
</script>
<form>
<input type="button" value="Click here" onclick="btnClick()">
</form>

onLoad

<body onload="Function_name()">
<frameset onload="Function_name()">

onUnload

<body onunload="alert(GoodBye)">

Multiple actions in an events can be separated using ";"

onclick="btnClcik();alert("Welcome")"

onFocus, onBlur and onChange

<input type="text" onChange="function()">

The above OnChange event will be called when the text is changed..
Likewise the OnFocus and OnBlur events

OnBlur is fired when the textbox or object loses focus

<input type="text" onblur="validateValue(this)">

onSubmit

It is used before submitting the form

<form method="post" action="MyPage.html"
onsubmit="function()">

onMouseOver and onMouseOut

Used to create animated buttons..Fires when the mouse is over the object.

onMouseOver="window.status='Click here to goto MyPAge.html'; return true"

OnKeyUp,OnKeyDown,OnKeyPress,OnDblClick(double click)

Cookies

Cookies are stored in the client side.

function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i ( ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

function eraseCookie(name) {
createCookie(name,"",-1);
}

0 comments:

Post a Comment