0

We are dynamically adding script tags to a page using HtmlTextWriter, which works great. We have a few that need to have async keyword added and I'm not sure how to do it.

I want the tag to look like this.

<script id="my_script"  async   type="text/javascript"  src="myscript.js"></script>

My method that builds the tags look like this.

    internal static void RenderJavaScriptInclude(HtmlTextWriter writer, string      filePath, string Id)
{
    writer.AddAttribute(HtmlTextWriterAttribute.Id, Id);
    writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/javascript");
    writer.AddAttribute(HtmlTextWriterAttribute.Src, filePath);
    writer.RenderBeginTag(HtmlTextWriterTag.Script);
    writer.RenderEndTag();
}

How can I modify to add "async"?

Much thanks as always,

Rhonda

Rhonda
  • 985
  • 5
  • 18
  • 32
  • What do you need to make async? You can add the `async` decorator to `RenderJavaScriptInclude` but I don't see where you require it. – Peter Ritchie Dec 20 '13 at 21:50
  • I'm integrating a third party application and they require it to be in the tag exactly like this. This is from the integration document. YourWebsite.com ...the standard document body of your site… – Rhonda Dec 20 '13 at 21:55
  • Ahh, right in the output. As far as I know the format should be `async="async"` Which is basically writer.AddAttribute("async", "async"). as far as I can tell this will do what you want (i.e. output the attribute without the value. Otherwise, have you tried `writer.AddAttribute("async", string.Empty)` or `writer.AddAttribute("async", null)`? – Peter Ritchie Dec 20 '13 at 22:02

1 Answers1

0

According to the source code of the RenderBeginTag method, any attribute with value equals to null (but not String.Empty) will be rendered without quoted value. So, just call writer.AddAttribute("async", null); before calling of RenderBeginTag.

Eugene Berdnikov
  • 2,150
  • 2
  • 23
  • 30