ASP. NET provides three backend JS output methods:
I. Output existing JS files in the background
First create the JS file testjs. js
Below isCodeFragment: If (! Page. clientscript. isclientscriptincluderegistered (this. GetType (), "keys") // you can check whether keys has been registered. { Page. clientscript. registerclientscriptinclude ("keys", "testjs. js "); } |
Ii. Output js code block
The following is a code snippet: String scriptstrs = ""; // This is only for demonstration. If the code needs to be spliced multiple times, use stringbuilder. Scriptstrs + = "function test (STR )"; Scriptstrs + = "{alert (STR );}"; If (! Page. clientscript. isclientscriptblockregistered (this. GetType (), "keys ")) { Page. clientscript. registerclientscriptblock (this. GetType (), "keys", scriptstrs, true ); } |
3. Output one-time JS Code
The following is a code snippet: String scriptstrs = ""; If (! Page. clientscript. isstartupscriptregistered (this. GetType (), "welcome ")) { Page. clientscript. registerstartupscript (this. GetType (), "welcome", scriptstrs ); } |
In addition, the response. Write (""); method can also output simple js code, but I personally do not advocate this method. In the past development, this method may cause the page font size to change after the prompt message is displayed. Therefore, we recommend that you use the above three methods to ensure security.