1. Call javascript Functions directly at the front-end
It's easy to add a script element between the head element and set the type element to "text/javascript"
For example:
Copy codeThe Code is as follows:
<Head runat = "server">
<Script type = "text/javascript">
Function ShowName (str)
{
Alert ("Your name is :(" + str + ")");
}
</Script>
<Title> using javascript </title>
</Head>
Then, access javascript Functions through events between body elements. For example, Access javascript Functions through the onclientclick event of button1.
Example:
Copy codeThe Code is as follows:
<Asp: Button ID = "Button1" runat = "server" Text = "Button" onclientclick = "ShowName ('xxx')"/>
When you click a button to run the project, "Your name is XXX" is displayed"
This is a simple javascript function.
2. Calling through js files on the front-end
The method is the same as (1), but you only need to specify the. js file.
Example:
Copy codeThe Code is as follows:
<Head runat = "server">
<Script type = "text/javascript" src = "JScript. js">
</Script>
<Title> using javascript </title>
</Head>
Then, access javascript Functions through events between body elements. For example, Access javascript Functions through the onclientclick event of button1.
Example:
// The ShowName method must be included in the. js file.
<Asp: Button ID = "Button1" runat = "server" Text = "Button" onclientclick = "ShowName ('xxx')"/>
3. Call javascript Functions in the background. The functions are in the. js file.
Head element at the front end
Copy codeThe Code is as follows:
<Head runat = "server">
<Script type = "text/javascript" src = "JScript. js">
</Script>
<Title> using javascript </title>
</Head>
The following code must be added to the background:
Button1.Attributes. Add ("onclick", "showname1 (XXX )");
4. Call javascript Functions in the background. The functions are written in the. js file, but are not defined in the foreground.
Copy codeThe Code is as follows:
// Obtain the. js File
String myscript = "JScript. js ";
// Register the. js file. If you view the source code, you will get the following code:
// <Script> src = "JScript. js" type = "text/javascript"> <script>
Page. ClientScript. RegisterClientScriptInclude ("myKey", myscript );
// Same as above
Button1.Attributes. Add ("onclick", "showname1 (123 )");
5. Write the script using the Response. Write Method
For example, after you click the button, you must first operate the database. After the operation is complete, it is displayed that the operation is complete. You can write it in the last place you want to call.
Response. Write ("<script type = 'text/javascript '> alert (); </script> ");
This method has a defect that the user-defined functions in the script file cannot be called, and only internal functions can be called. The specific user-defined functions can only be called in Response. write the function definition, such as Response. write ("<script type = 'text/javascript '> function myfun (){...} </script> ");
6. dynamically add scripts using the ClientScript class
The usage is as follows: add code where you want to call a javascript script function. Make sure that MyFun has been 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 UDF in the script file.
Note: In all the above methods, the background Code cannot convert the code of the current page, such as Redirect. You need to put the page conversion code in the script.