Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
where to write javascript in asp.net
Posted

There are multiple possibilities.

1) Without Master-Content Pages.

Either you can add your JavaScript code inside head tag of your Aspx page.
ASP.NET
<head  runat="server">
    <title>Test</title>

    <script type="text/javascript">

        function Test() {
            alert('Hi');
        }

 </script>

</head>


Or add new .js file in your Project, write your JavaScript code inside it and then reference it in your Aspx page.
ASP.NET
<head runat="server">
    <title>Test</title>

<script language="javascript" type="text/javascript" src="Scripts/MyTest.js"></script>

</head>


2) With Master-Content Pages.

Calling JavaScript from ASP.NET Master Page and Content Pages - Part I

Calling JavaScript from ASP.NET Master Page and Content Pages - Part II
 
Share this answer
 
v2
Comments
sunitha korakandla 5-Sep-13 1:40am    
Can we Write the java script function at the end of ur aspx page...?
If Possible please let me know
The Doer 20-Jan-15 2:10am    
Yes Sunitha, that is the best practice to write your javascript just before your <body> tag ends.
There are a number of places...


* You can write script functions directly in the .aspx pages

* You can write them in a .js file, then set a reference to the .js file from your page \ master page

* You can use RegisterClientScriptBlock \ RegisterStartupScript in your server pages


Personally, I prefer the second option, makes it easier to manage in my opinion
 
Share this answer
 
write it under <Head> </Head> tags

e.g.
HTML
<head>
<script language="Javascript">
function check()
{
  alert("i am in head");
}
</script>
</head>
 
Share this answer
 
Inside <head> </head>or <body> </body>tag of your aspx page markup.

you can also add it at runtime by Page.RegisterStartupScript() from code behind, after all this script is also going to be inside the <body></body> tag of your page..

HTML
<body>
  <script language="javascript">
  </script>
</body>


follow the links to learn in detail

http://msdn.microsoft.com/en-us/library/aa479011.aspx[^]

[Video tutorial^]
 
Share this answer
 
C#
<head runat="server">
<script type="text/javascript" language="javascript">
function Count() 
{
	//
}

Check this
MSDN
 
Share this answer
 

function Test()
{
alert('Hi');
}





Write write java script in asp.net between head tag.
 
Share this answer
 
Refer this link it may help you.. :)

http://msdn.microsoft.com/en-us/library/aa479011.aspx
 
Share this answer
 
Comments
Ronjon1 9-Jan-13 6:11am    
How can I use javascript function in c# code? Please tell me the proper way.
MOHI UDDIN

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900