Wednesday, 14 March 2012

Convert unix date to datetime.

Definition: Convert Unixdate to DateTime stamp.

protected void Button1_Click(object sender, EventArgs e)
    {
        // UNIX timestamp
        double timestamp = 1309177737;

        // Step1 : Make System.DateTime equivalent to the UNIX Epoch.

        System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);

        // Step 2: Add the number of seconds in UNIX timestamp to be converted.

        dateTime = dateTime.AddSeconds(timestamp);

        // The dateTime now contains the right date/time so to format the string,

        // use the standard formatting methods of the DateTime object.

        string printDate = dateTime.ToShortDateString() + " " + dateTime.ToShortTimeString();

        // Print the date and time
        Response.Write(printDate);
    }

Output :
6/27/2011 12:28 PM

Wednesday, 7 March 2012

How to add click event to Anchor or tag?

Definition: 
In script part you can add click event to Achor or <a></a> tag. With the help of Jquery you can do this. First download the following jquery (jquery-1.4.3.min.js) if you don't have.Second add jquery.

To Add use following code: 
<script src="Jquery/jquery-1.4.3.min.js" type="text/javascript"></script>
(please take care of path where you have save jquery)
Example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <title>Clcik event</title>
<script src="Jquery/jquery-1.4.3.min.js" type="text/javascript"></script>

    <script type="text/javascript">
       $(document).ready(function(){
             $("a").click(function(event){
             alert("Thanks for visiting!");
          });
        });
    </script>
  </head>
  <body>
    <a href="http://arcreview.blogspot.in/">Arc's Review</a>
  </body>
</html>
Output:
You will see alert box and you press ok button , you will redirect to my blog.

Monday, 5 March 2012

How to insert tab in JavaScript?

Definition: "\t" will insert tab in the text.

Example:
 
Script Part:
 
   function show()
        {
         
           var text = "how to insert tab \t 123456789";
            document.getElementById("txtsp").value = text;
          
        }

Control Part:
       <textarea id="txtsp" name="txtsp" class="style1" ></textarea>
        <input type="submit" name="show" id="show"  value="Special Character" onclick="show();"/>

Output:
how to insert tab      123456789

How to insert Single Quote in Javascript?

Definition: To insert ('') single quote in JavaScript, use ( \ ) backslash.
The backslash ( \ ) is used to insert apostrophes, new lines, quotes, and other special characters into a text string.

Example:
Script Part:

 <script type="text/javascript" language="javascript">
        function show()
        {
                     var text ="how to use \'single quote\'";
                    document.getElementById("txtSpecial").value = text;
         }
  </script>

HTML Control:

        <input value="" id="txtSpecial" name="txtSpecial" type="text"/><br />
        <input type="submit" name="show" id="show"  value="Special Character" onclick="show();"/>



Output:
how to use 'single quote'

Friday, 2 March 2012

setTimeout() Method Javascript

Definition:
setTimeout() - executes a code some time in the future

Syntax:
var tTime = setTimeout("javascript statement",milliseconds);

The first parameter of setTimeout() can be a string of executable code, or a call to a function.
The second parameter indicates how many milliseconds from now you want to execute the first parameter.

1000 milliseconds = one second

Example:

When the button is clicked in the example below, an alert box will be displayed after 2 seconds.

 <html>
<head>
<script type="text/javascript">
function timeMessage()
{
var tTime = setTimeout("HelloWorld()",2000);
}
function HelloWorld()
{
         alert("Hello World");
}
</script>
</head>

<body>
<form>
<input type="button" value="Display alert box in 2 seconds" onclick="timeMessage()" />
</form>
</body>
</html>

Thursday, 19 January 2012

Access and Modify values of an Array?

Definition: The Array object is used to store multiple values in a single variable.

Example:

First  Create Array (To learn more about how to create array in more detail , Please click the following link) http://arcreview.blogspot.com/2012/01/how-to-create-array-in-javascript.html

var myFruits=new Array(); 
myFruits[0]="Mango";     
myFruits[1]="Orange";
myFruits[2]="Pineapple";

Access an Array

You can refer to a particular element in an array by referring to the name of the array and the index number. The index number starts at 0.
The following code line:

document.write(myFruits[0]);

o/p will be:
 Mango

Modify Array 

To modify a value in an existing array, just add a new value to the array with a specified index number.

myFruits[0]="Grapes";

The following code line:
document.write(myFruits[0]);

o/p will be:
Grapes


How to create Array in JavaScript?

Definition : The Array object is used to store multiple values in a single variable.

Create an Array
An array can be defined in three ways. 

The following code creates an Array object called myFruits:

1.
var myFruits=new Array(); // regular array (add an optional integer
myFruits[0]="Mango";       // argument to control array's size)
myFruits[1]="Orange";
myFruits[2]="Pineapple";


2.
var myFruits=new Array("Mango","Orange","Pineapple"); // condensed array


 3.
var myFruits=["Mango","Orange","Pineapple"]; // literal array


Note: If you specify numbers or true/false values inside the array then the variable type will be Number or Boolean, instead of String.

Example:
HTML Body tag:


<body>
    <form id="form1" runat="server">
    <div>
        <input id="btnCreateArray" type="submit" value="Create Array" name="
btnCreateArray"   onclick="CreateArray();"/>
     </div>
    </form>
</body>



JavaScript Code:
<script type="text/javascript" language="javascript">
       var myarray = new Array();
       

        function CreateArray()
        {
           
            myarray[0]= "apple";
            myarray[1]="orange";
            myarray[2] ="grapes";
           
         document.write(myarray[0] + "  " + myarray[1] + "  " + myarray[2]);
        }
        CreateArray();
                   
    </script>