简体   繁体   中英

How do I add an attribute to the script tag when using RegisterClientScriptInclude in c#

I'm trying to have my script tags include the attribute defer="true" when used like so

string path = "~/scripts/v_wall.js";
Page.ClientScript.RegisterClientScriptInclude(typeof(SlideShow), "defaultslideshow", ResolveUrl(path));

How can I have this method render the script tag like so?

<script defer = "true" type="text/javascript">

     <!-- etc... -->

</script>

Many thanks!

Why not to use the method RegisterClientScriptBlock instead? http://msdn.microsoft.com/en-us/library/btf44dc9.aspx

I guess that should be something like..

string scriptstr = "<script defer='true' type='' src=''></script>";
Page.ClientScript.RegisterClientScriptBlock(typeof(SlideShow), "defaultslideshow", scriptstr);

Good luck

I think that you will need to emit the script tags yourself. In order to get it added to the page as early as possible in case that something in the page needs the javascript as soon as possible.

To do this, you can add them to the page header:

var sbText = new System.Text.StringBuilder(500);
// ToDo: Add your script to the textbuilder here
this.Page.Header.Controls.Add(New LiteralControl(sbText.ToString()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM