Usually javascript code can be placed directly in the front-end page together with HTML tags, but if there is a lot of JS code, it is not conducive to maintenance on the one hand, and it is not friendly to search engines on the other hand, because the page becomes bloated.
So generally programmers with good development habits will put the javascript code into a separate js file, and other pages will use the corresponding javascript code by introducing the js file.
Use the following method to reference the JS file: .
ASP.NET itself provides a variety of methods for calling javascript scripts. The author summarizes six calling methods here. You can choose the corresponding calling method according to your own usage habits!
1. Call the custom javascript function directly on the front page:
It’s very simple. Add the script element between the head elements and set the type element to "text/javascript", such as:
<head runat="server"> <script type="text/javascript" > function ShowName(str) { alert("十万个为什么的站长是:("+str+")"); } </script> <title>using javascript</title> </head>
Then, access through events between the body elements. For example, the example of accessing the ShwoName() of the javascript function through button1's click event (onclientclick) is as follows:
<asp:Button ID="Button1" runat="server" Text="Button" onclientclick="ShowName('伍')" />
Run the project at this time, When the button is clicked, "One Hundred Thousand Whys, the webmaster is: Wu" is displayed. This is to directly define and call the javascript function on the front-end page.
2. Call the corresponding function by introducing js files in the front-end Function:
The method is the same as (1), except that you need to specify the js file to be called.
The example is as follows:
<head runat="server"> <script type="text/javascript" src="kenscript.js"> </script> <title>using javascript</title> </head>
Then access through events between the body elements, such as through button1 An example of clicking the event (onclientclick) to access the javascript function is as follows:
//此时 .js文件中必须有 ShowName 方法 <asp:Button ID="Button1" runat="server" Text="Button" onclientclick="ShowName('伍')" />
3. Call the javascript function in the background, and the function is in the head element of the foreground in the .js file
<head runat="server"> <script type="text/javascript" src="kenscript.js"> </script> <title>using javascript</title> </head> //后台的需要添加如下代码 Button1.Attributes.Add("onclick", "showname1(‘伍’)");
4 , call the javascript function in the background. The function is written in the corresponding js file, but is not defined in the foreground. The example is as follows:
//获得.js文件 string myscript = "kenscript.js"; //注册.js文件 Page.ClientScript.RegisterClientScriptInclude("myKey", myscript); 如果此时查看源码,会得到如下代码 //<script src ="kenscript.js" type="text/javascript"><script> //同上 Button1.Attributes.Add("onclick", "showname1('伍')");
5. Use the Response.Write method to write the script
For example, in your single After clicking the button, the database will be operated first. After the completion, it will be displayed as completed. You can write
Response.Write("<script type='text/javascript'>alert("操作完成!");</script>");
in the last place you want to call. However, this method has a flaw in that it cannot call the custom function in the script file. Only internal functions can be called. To specifically call a custom function, you can only write the function definition in Response.Write,
For example, Response.Write("");
6. Use the ClientScript class to dynamically add scripts
The usage is as follows: add code where you want to call a javascript script function, be sure to ensure that MyFun Already defined in the script file.
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>MyFun();</script>");
This method is more convenient than Response.Write. You can directly call the custom function in the script file, but it is not as simple and intuitive as the previous ones.
Note that in all the above methods, the background code cannot have code to convert the current page, such as Redirect, etc. The page conversion code must be placed in the script
The above is the detailed content of How to use js files in asp.net. For more information, please follow other related articles on the PHP Chinese website!