【问题标题】:Adding async keyword to script tag using HtmlTextWriter使用 HtmlTextWriter 将 async 关键字添加到脚本标记
【发布时间】:2014-01-09 20:04:32
【问题描述】:

我们正在使用 HtmlTextWriter 向页面动态添加脚本标签,效果很好。我们有一些需要添加 async 关键字,我不知道该怎么做。

我希望标签看起来像这样。

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

我构建标签的方法如下所示。

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

如何修改以添加“异步”?

非常感谢,

朗达

【问题讨论】:

  • 异步需要什么?您可以将 async 装饰器添加到 RenderJavaScriptInclude 但我看不到您需要它的位置。
  • 我正在集成第三方应用程序,他们要求它完全像这样在标签中。这是来自集成文档。 EN" YourWebsite.com ...您网站的标准文档正文...
  • 啊,就在输出中。据我所知,格式应该是async="async",基本上是 writer.AddAttribute("async", "async")。据我所知,这会做你想做的(即输出没有值的属性。否则,你试过writer.AddAttribute("async", string.Empty)或writer.AddAttribute("async", null)吗?

标签: htmltextwriter


【解决方案1】:

根据RenderBeginTag方法的源代码,任何值等于null(但不是String.Empty)的属性都将在不带引号的情况下呈现。因此,只需在调用 RenderBeginTag 之前调用 writer.AddAttribute("async", null);。

【讨论】:

    猜你喜欢
    • 2019-09-14
    • 2021-07-11
    • 2017-03-01
    • 2017-08-16
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多