Thursday 26 April 2012

How to convert string to number in javascript?

Definition: Convert String to Number.

The Number() function converts the object argument to a number that represents the object's value.
If the value cannot be converted to a legal number, NaN is returned.
In short,
Number() method will convert string to number.

Example A:
Javascript function
 
function add(a,b)
{
    var ans;
    var i = a;
    var j = b;
    ans = Number(a) + Number(b);
 
    alert("Your answer: " + ans);
}

 <div>
        <input type="text" id="txta" />
        <input type="text" id="txtb" />
        <input type="button" id="btnAdd" value="Addition" onclick="add(document.getElementByI('txta').value,document.getElementById('txtb').value);" />
          </div>
Output:



Example B:
<script type="text/javascript" language="javascript">

var test1= new Boolean(true);
var test2= new Boolean(false);
var test3= new Date();
var test4= new String("999");
var test5= new String("999 888");

document.write(Number(test1)+ "<br />");

document.write(Number(test2)+ "<br />");
document.write(Number(test3)+ "<br />");
document.write(Number(test4)+ "<br />");
document.write(Number(test5)+ "<br />");

</script> 

Output:
1
0
1335442904505
999
NaN

Saturday 21 April 2012

How to call external javascript file in asp.net?

Definition:
Call external javascript file.

First create javascript file. Add JScript.js file in your solution.
No need to add <script> tag in this file.

In <head> tag add following line of code.
<script type="text/javascript" src="../JScript.js"></script>

src= source path of your JScript.js file.

Example:
Following example shows how to call or add external javascript file.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>External File</title>
    <script type="text/javascript" src="../JScript.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="text" id="txta" />
        <input type="text" id="txtb" />
        <input type="button" id="btnAdd" value="Addition" onclick="add(document.getElementById('txta').value),document.getElementById('txtb').value);" />
      </div>  
    </form>
</body>
</html>


JScript.js file  (don't add <script> tag in .js file)
function add(a,b)
{
    var ans;
    var i = a;
    var j = b;
    ans = Number(a) + Number(b);
 
    alert("Your answer: " + ans);
}

Output:
Addition of two text box will be shown.




 

Wednesday 18 April 2012

How to show alert box in javascript?

Definition:
The alert() method displays an alert box with a specified message and an OK button.

Syntax:
alert(your message)

you can give any appropriate message.

Example:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
      <title>Alert Box Example</title>
        <script type="text/javascript" language="javascript">
             function show()
            {
                alert("Hello World!");
            }
        </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        <input type="button" id="alertshow" name="alertshow"         onclick="show();" value="Alert Box" />
    </div>
    </form>
</body>
</html>

Output: