Programming in almost language

This is the site where you may share your knowledge and experience to eachother..

  • Categories

  • LinkedIn

  • Tweet Me

  • My footsteps

Archive for the ‘JAVA SCRIPT TUTORIAL’ Category

Java script

Posted by Praveen Kumar on March 3, 2008

JavaScript Key Code

First try to get the key code whether its returning some code or not

alert(event.keyCode);

Press any key to determine the javascript key code of that key. This is a simple script:

<script language=”JavaScript”>
document.onkeydown = checkKeycode
function checkKeycode(e) {
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
alert(“keycode: ” + keycode);
}
</script>

Auto Refresh page after clicking on close/submit button

<a href=”javascript:window.opener.location.reload();javascript:window.close();”>

<input type=”button” value=”Close” />

</a>

this will close popup after clicking save button of popup and refresh the parent form.

Advertisements

Posted in JAVA SCRIPT TUTORIAL | Leave a Comment »

Create cookies using JavaScript

Posted by Praveen Kumar on February 22, 2008

Create and Store a Cookie
First, we create a function that stores the name of the visitor in a cookie variable

function setCookie(c_name,value,expiredays)
{var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ “=” +escape(value)+
((expiredays==null) ? “” : “;expires=”+exdate.toGMTString());
}

Then, we create another function that checks if the cookie has been set

function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + “=”);
if (c_start!=-1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(“;”,c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return “”;
}

Last, we create the function that displays a welcome message if the cookie is set, and if the cookie is not set it will display a prompt box, asking for the name of the user

function checkCookie()
{
username=getCookie(‘username’);
if (username!=null && username!=””)
{
alert(‘Welcome again ‘+username+’!’);
}
else
{
username=prompt(‘Please enter your name:’,””);
if (username!=null && username!=””)
{
setCookie(‘username’,username,365);
}
}
}

All together now

<html>
<head>
<script type=”text/javascript”>
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + “=”);
if (c_start!=-1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(“;”,c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return “”;
}function setCookie(c_name,value,expiredays)
{var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ “=” +escape(value)+
((expiredays==null) ? “” : “;expires=”+exdate.toGMTString());
}function checkCookie()
{
username=getCookie(‘username’);
if (username!=null && username!=””)
{
alert(‘Welcome again ‘+username+’!’);
}
else
{
username=prompt(‘Please enter your name:’,””);
if (username!=null && username!=””)
{
setCookie(‘username’,username,365);
}
}
}
</script>
</head><body onLoad=”checkCookie()”>
</body>
</html>

Posted in JAVA SCRIPT TUTORIAL | Leave a Comment »