The best way to add JavaScript at the end of ASP. Net

Source: Internet
Author: User

How do I add JavaScript scripts or libraries to the end of the asp.net page before the end mark of the page? Several methods are summarized for reference.

1. Use RegisterClientScriptInclude to better reference the JavaScript Library (JsFile. js:

If (! Page. ClientScript. IsClientScriptIncludeRegistered ("jsFileInclude "))
Page. ClientScript. RegisterClientScriptInclude ("jsFileInclude", "JsFile. js ");
Add a JavaScript library reference at the beginning of the viewstate data document.

2. to insert some JavaScript code into the page, you can use the RegisterStartupScript method:

String jsCodeBlock = "var MyStr = 'where'; alert (MyStr );";

If (! Page. ClientScript. IsStartupScriptRegistered ("myJsCode "))
Page. ClientScript. RegisterStartupScript (typeof (string), "myJsCode", jsCodeBlock, true );
The Javascript code is added to the end of the document.
When the last parameter is set to true ,. the. net Framework is automatically added to the start and end of the script tag (or merged with other generated JavaScript code based on the same script tag ).

However, we can also use the RegisterStartupScript method to load the reference to the JavaScript Library at the end of the document, write the complete js file, and set the last parameter to false:
String jsFile = "<script src = \" JsFile. js \ "Type = \" text/javascript \ "> </script> ";

If (! Page. ClientScript. IsStartupScriptRegistered ("myJsFileRef "))
Page. ClientScript. RegisterStartupScript (typeof (string), "myJsFileRef", jsFile, false );

Related Article

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.