ASP. NET provides three backend JS output methods

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.