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.




 

No comments:

Post a Comment