Asp. The best way to add JavaScript to the end of the net page real-life sharing _ practical skills

Source: Internet
Author: User
How do I add a JavaScript script or library to the end of a asp.net page before the end tag of the page? Several methods are summarized as reference
1 A better reference to the JavaScript library (jsfile.js) using registerclientscriptinclude:
Copy Code code as follows:

if (! Page.ClientScript.IsClientScriptIncludeRegistered ("Jsfileinclude"))
Page.ClientScript.RegisterClientScriptInclude ("Jsfileinclude", "jsfile.js");

Add a JavaScript library reference at the beginning of the document ViewState data.
2 to insert some JavaScript code into the page, you can use the RegisterStartupScript method:
Copy Code code as follows:

String jscodeblock = "var mystr= ' here"; 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 the true,.net frame, it is automatically added to the start and end of the script tag (or merge with the other generated JavaScript code, based on the same script tag).
But we can also use the RegisterStartupScript method to load a reference to the JavaScript library at the end of the document, we write the complete JS file, and set the last argument to false:
Copy Code code as follows:

String jsfile = "<script src=\" jsfile.js\ "type=\" text/javascript\ "></script>";
if (! Page.ClientScript.IsStartupScriptRegistered ("Myjsfileref"))
Page.ClientScript.RegisterStartupScript (typeof (String), "Myjsfileref", Jsfile, false);

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.