使用HAML为javascript指定async属性

问题描述 投票:0回答:2

为了在网页中包含谷歌分析生成

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-135345611-1">

我在docs找不到那么多。然后我找到了this,但它将我的HTML标签转换为

%script(:async => "", src="https://www.googletagmanager.com/gtag/js?id=UA-135345611-1")

这使得中间人v4.2.1失败了:

Invalid attribute list: "(:async => \"\", src=\"https://www.googletagmanager.com/gtag/js?id=UA-135345611-1\")".
partials/_header.html.haml:4

任何人都知道如何在HAML中编写此行,以便正确解析?

javascript haml middleman
2个回答
1
投票

你试过这个吗?

%script{:async => "", :src => "https://www.googletagmanager.com/gtag/js?id=UA-135345611-1"}

当我通过该站点运行您的代码时,它用括号而不是parens包装它。也许这就是为什么它没有正确解析?

另外,我个人使用https://html2haml.herokuapp.com/,因为它使用的是中间人使用的相同版本的haml。


0
投票

要呈现没有属性值的HTML属性,例如asyncselected,请在http://haml.info/docs/yardoc/file.REFERENCE.html#attributes滚动到“布尔属性”。

因此,要包含您的GA代码,请使用:

%script{async: true, src: "https://www.googletagmanager.com/gtag/js?id=UA-135345611-1"}
© www.soinside.com 2019 - 2024. All rights reserved.