是不是async和defer语法可以async =" async"?

时间:2016-02-01 20:46:05

标签: javascript c#

我有皮肤DNN。我的头标签有runat =" server"所以我尝试在head标签中添加一个标签,如此

<script async defer src="https://maps.googleapis.com/maps/api/js?key=<%#GoogleAPIkey%>"></script>

在后面的代码中我在一个属性中设置了var GoogleAPIkey。 问题是它没有加载值。

然后我尝试在代码中添加脚本标记作为此

    HtmlGenericControl jqueryInclude = new HtmlGenericControl("script");
    jqueryInclude.Attributes.Add("type", "text/javascript");
    jqueryInclude.Attributes.Add("async", "async");
    jqueryInclude.Attributes.Add("defer", "defer");
    jqueryInclude.Attributes.Add("src", "https://maps.googleapis.com/maps/api/js?key=" + GoogleAPIkey );

此方法将标记输出为

<script async="async" defer="defer" src="https://maps.googleapis.com/maps/api/js?key=xxx"></script>

是这种有效的语法还是我需要使用不同的方法来编写它 - 或者甚至将它移出头标记。

1 个答案:

答案 0 :(得分:4)

规范指出async is a boolean attribute。至于如何指定boolean attributes

  

元素上存在布尔属性表示真值,缺少属性表示假值。

     

注意:值&#34; true&#34;和&#34;假&#34;不允许在布尔属性上。要表示错误值,必须完全省略该属性。

从技术上讲,重要的是属性存在,而不是它的价值。这意味着您可以使用 count name group a g1 1 g2 4 b g1 2 g2 3