1. Call JavaScript function directly at foreground
It's simple to add a script element between the head elements and set the type element to "Text/javascript"
Such as:
Copy Code code as follows:
<script type= "Text/javascript" >
function ShowName (str)
{
Alert ("Your name is: (" +str+ ")");
}
</script>
<title>using javascript</title>
Then, between the body elements, you access the JavaScript function through the event to access the Button1 Click event (OnClientClick)
Examples are as follows:
Copy Code code as follows:
<asp:button id= "Button1" runat= "Server" text= "button" onclientclick= "ShowName (' XXX ')"/>
When you run the project, when you click the button, "Your name is XXX" is displayed.
This is a simple JavaScript function.
2, in the foreground through the JS file call
Method is the same as (1) except that you need to specify a. js file
Examples are as follows:
Copy Code code as follows:
<script type= "Text/javascript" src= "Jscript.js" >
</script>
<title>using javascript</title>
Then, between the body elements, you access the JavaScript function through the event to access the Button1 Click event (OnClientClick)
Examples are as follows:
There must be a ShowName method in the. js file at this time
<asp:button id= "Button1" runat= "Server" text= "button" onclientclick= "ShowName (' XXX ')"/>
3, in the background call JavaScript functions, Functions in the. js file
Front of the head element
Copy Code code as follows:
<script type= "Text/javascript" src= "Jscript.js" >
</script>
<title>using javascript</title>
Background needs to add the following code
BUTTON1.ATTRIBUTES.ADD ("onclick", "showname1 (XXX)");
4, in the background call JavaScript functions, functions written in the. js file, but not in the foreground definition
Copy Code code as follows:
Get. js files
String myscript = "Jscript.js";
Register. js file, if you look at the source, you will get the following code
<script> src = "jscript.js" type= "Text/javascript" ><script>
Page.ClientScript.RegisterClientScriptInclude ("MyKey", myscript);
Ditto
BUTTON1.ATTRIBUTES.ADD ("onclick", "showname1 (123)");
5. Write script with Response.Write method
For example, after you click the button, the first operation of the database, after the display has been completed, you can finally want to call the place to write
Response.Write ("<script type= ' Text/javascript ' >alert ();</script>");
A flaw in this approach is that you cannot call custom functions in script files, only internal functions, and specific calls to custom functions can only be written in Response.Write, such as Response.Write ("<script type=" text/ JavaScript ' >function myfun () {...} </script> ");
6. Dynamically adding scripts with the ClientScript class
Use the following: To add code where you want to invoke a JavaScript script function, be aware that Myfun has already been defined in the script file.
Clientscript.registerstartupscript (Clientscript.gettype (), "MyScript", "<script>myfun ();</script>");
This method is more convenient than Response.Write, and you can call the custom function in the script file directly.
Note that all of the above methods, the background code can not be converted to the current page code, such as redirect, and so on, to put the transfer page code in the script