在后面的代码中,我在属性中设置了 var GoogleAPIkey。问题是它-6ren">
gpt4 book ai didi

javascript - async 和 defer 语法可以是 async ="async"吗?

转载 作者:行者123 更新时间:2023-11-29 10:09:57 27 4
gpt4 key购买 nike

我有带皮肤的 DNN。我的 head 标签有 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>

这是有效的语法吗,还是我需要采用不同的方法将其写出 - 或者甚至可能将其移出 head 标签。

最佳答案

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

The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.

If the attribute is present, its value must either be the empty string or [...] the attribute's canonical name, with no leading or trailing whitespace.

Note: The values "true" and "false" are not allowed on boolean attributes. To represent a false value, the attribute has to be omitted altogether.

所以从技术上讲,重要的是属性是否存在,而不是它的值是多少。这意味着您可以使用 async="async"

关于javascript - async 和 defer 语法可以是 async ="async"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35139976/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com