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 );