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:
<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:
<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:
<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
<Head runat = "server">
<SCRIPT type = "text/JavaScript" src = "jscript. js">
</SCRIPT>
<Title> using JavaScript </title>
</Head>
The following must be added to the background:Code
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.
// 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 )");