这种语言特性仅在ECMASCRIPT_2015模式或更高版本中受支持:块作用域函数声明

huangapple go评论64阅读模式
英文:

This language feature is only supported for ECMASCRIPT_2015 mode or better: block-scoped function declaration

问题

我有两个横幅需要在我的HTML中的一个div存在时显示,我已经创建了这个脚本,它完美地工作。问题是我正在使用GTM,它给出了这个错误:“此语言功能仅支持ECMASCRIPT_2015模式或更高版本:块作用域函数声明。” 我该如何将我的函数转换为GTM接受的代码?

var element = document.getElementById('chamaBannerVB');
var elementZC = document.getElementById('chamaBannerZC');
if (element !== null) {
    function appendHtmlVB() {
        var divVB = document.getElementById("chamaBannerVB");
        divVB.innerHTML += '<a href="https://vidabalanceada.com.br/facaparte?mktcode=IDSTCS" target="_blank"><img class="bannerEbookVB" src="https://img.selecoesbrasil.com.br/banner_ebook/mobile_banner_ebook_vb_LS_03-2023.png" alt="Banner Mobile" style="width: 100%; max-width: 480px;"></a>';
        var styleSheet = document.createElement("style");
        styleSheet.innerText = styles;
        document.querySelector(".bannerEbookVB").appendChild(styleSheet);
    }
    window.onload = appendHtmlVB;
    var styles = "@media (min-width: 768px) {" + ".bannerEbookVB {" + "content: url(https://img.selecoesbrasil.com.br/banner_ebook/banner_ebook_vb_LS_03-2023.png); max-width: 800px!important;" + "}}";
} else {
    function appendHtml() {
        var div = document.getElementById("chamaBannerZC");
        div.innerHTML += '<a href="https://zenconecta.com.br/facaparte?mktcode=IZSTCS" target="_blank"><img class="bannerEbook" src="https://img.selecoesbrasil.com.br/banner_ebook/mobile_banner_ebook_zc_LS_03-2023.png" alt="Banner Mobile" style="width: 100%; max-width: 480px;"></a>';
        var styleSheet = document.createElement("style");
        styleSheet.innerText = styles;
        document.querySelector(".bannerEbook").appendChild(styleSheet);
    }
    window.onload = appendHtml;
    var styles = "@media (min-width: 768px) {" + ".bannerEbook {" + "content: url(https://img.selecoesbrasil.com.br/banner_ebook/banner_ebook_zc_LS_03-2023.png); max-width: 800px!important;" + "}}";
}
<div id="chamaBannerZC" style="text-align: center; display: flex; justify-content: center; margin: 16px 0;"></div>
英文:

I have two banners that needs to be displayed when one of the divs exist on my HTML, I've made this script and it works perfectly. The problem is that I'm using GTM and it gives this error: "This language feature is only supported for ECMASCRIPT_2015 mode or better: block-scoped function declaration." How can I transform my function to a code that GTM accepts it?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

    var element = document.getElementById(&#39;chamaBannerVB&#39;)
var elementZC = document.getElementById(&#39;chamaBannerZC&#39;)
    if (element !== null) {
        function appendHtmlVB() {
            var divVB = document.getElementById(&quot;chamaBannerVB&quot;);
            divVB.innerHTML += &#39;&lt;a href=&quot;https://vidabalanceada.com.br/facaparte?mktcode=IDSTCS&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;bannerEbookVB&quot; src=&quot;https://img.selecoesbrasil.com.br/banner_ebook/mobile_banner_ebook_vb_LS_03-2023.png&quot; alt=&quot;Banner Mobile&quot; style=&quot;width: 100%; max-width: 480px;&quot;&gt;&lt;/a&gt;&#39;;
            var styleSheet = document.createElement(&quot;style&quot;)
            styleSheet.innerText = styles
            document.querySelector(&quot;.bannerEbookVB&quot;).appendChild(styleSheet);
        }       
        window.onload = appendHtmlVB;
        var styles = &quot;@media (min-width: 768px) {&quot; + &quot;.bannerEbookVB {&quot; + &quot;content: url(https://img.selecoesbrasil.com.br/banner_ebook/banner_ebook_vb_LS_03-2023.png); max-width: 800px!important;&quot; +&quot;}}&quot;

    } else {
        function appendHtml() {
            var div = document.getElementById(&quot;chamaBannerZC&quot;);
            div.innerHTML += &#39;&lt;a href=&quot;https://zenconecta.com.br/facaparte?mktcode=IZSTCS&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;bannerEbook&quot; src=&quot;https://img.selecoesbrasil.com.br/banner_ebook/mobile_banner_ebook_zc_LS_03-2023.png&quot; alt=&quot;Banner Mobile&quot; style=&quot;width: 100%; max-width: 480px;&quot;&gt;&lt;/a&gt;&#39;;
            var styleSheet = document.createElement(&quot;style&quot;)
            styleSheet.innerText = styles
            document.querySelector(&quot;.bannerEbook&quot;).appendChild(styleSheet);
        }       
        window.onload = appendHtml;
        var styles = &quot;@media (min-width: 768px) {&quot; + &quot;.bannerEbook {&quot; + &quot;content: url(https://img.selecoesbrasil.com.br/banner_ebook/banner_ebook_zc_LS_03-2023.png); max-width: 800px!important;&quot; +&quot;}}&quot;
    }

<!-- language: lang-html -->

<!-- <div id="chamaBannerVB" style="text-align: center; display: flex; justify-content: center; margin: 16px 0; "></div> -->

<div id="chamaBannerZC" style="text-align: center; display: flex; justify-content: center; margin: 16px 0; "></div>

<!-- end snippet -->

答案1

得分: 1

使用全局函数(在顶层作用域中声明)

function appendHtmlVB() {
    var divVB = document.getElementById("chamaBannerVB");
    divVB.innerHTML += '<a href="https://vidabalanceada.com.br/facaparte?mktcode=IDSTCS" target="_blank"><img class="bannerEbookVB" src="https://img.selecoesbrasil.com.br/banner_ebook/mobile_banner_ebook_vb_LS_03-2023.png" alt="Banner Mobile" style="width: 100%; max-width: 480px;"></a>';
    var styleSheet = document.createElement("style");
    styleSheet.innerText = styles;
    document.querySelector(".bannerEbookVB").appendChild(styleSheet);
}
function appendHtml() {
    var div = document.getElementById("chamaBannerZC");
    div.innerHTML += '<a href="https://zenconecta.com.br/facaparte?mktcode=IZSTCS" target="_blank"><img class="bannerEbook" src="https://img.selecoesbrasil.com.br/banner_ebook/mobile_banner_ebook_zc_LS_03-2023.png" alt="Banner Mobile" style="width: 100%; max-width: 480px;"></a>';
    var styleSheet = document.createElement("style");
    styleSheet.innerText = styles;
    document.querySelector(".bannerEbook").appendChild(styleSheet);
}

var element = document.getElementById('chamaBannerVB');
var elementZC = document.getElementById('chamaBannerZC');
if (element !== null) {
    window.onload = appendHtmlVB;
    var styles = "@media (min-width: 768px) {" + ".bannerEbookVB {" + "content: url(https://img.selecoesbrasil.com.br/banner_ebook/banner_ebook_vb_LS_03-2023.png); max-width: 800px!important;}}";
} else {
    window.onload = appendHtml;
    var styles = "@media (min-width: 768px) {" + ".bannerEbook {" + "content: url(https://img.selecoesbrasil.com.br/banner_ebook/banner_ebook_zc_LS_03-2023.png); max-width: 800px!important;}}";
}

或者根本不使用函数声明,直接将函数表达式分配给事件处理程序属性:

var element = document.getElementById('chamaBannerVB');
var elementZC = document.getElementById('chamaBannerZC');
if (element !== null) {
    window.onload = function appendHtmlVB() {
        var divVB = document.getElementById("chamaBannerVB");
        divVB.innerHTML += '<a href="https://vidabalanceada.com.br/facaparte?mktcode=IDSTCS" target="_blank"><img class="bannerEbookVB" src="https://img.selecoesbrasil.com.br/banner_ebook/mobile_banner_ebook_vb_LS_03-2023.png" alt="Banner Mobile" style="width: 100%; max-width: 480px;"></a>';
        var styleSheet = document.createElement("style");
        styleSheet.innerText = styles;
        document.querySelector(".bannerEbookVB").appendChild(styleSheet);
    };
    var styles = "@media (min-width: 768px) {" + ".bannerEbookVB {" + "content: url(https://img.selecoesbrasil.com.br/banner_ebook/banner_ebook_vb_LS_03-2023.png); max-width: 800px!important;}}";
} else {
    window.onload = function appendHtml() {
        var div = document.getElementById("chamaBannerZC");
        div.innerHTML += '<a href="https://zenconecta.com.br/facaparte?mktcode=IZSTCS" target="_blank"><img class="bannerEbook" src="https://img.selecoesbrasil.com.br/banner_ebook/mobile_banner_ebook_zc_LS_03-2023.png" alt="Banner Mobile" style="width: 100%; max-width: 480px;"></a>';
        var styleSheet = document.createElement("style");
        styleSheet.innerText = styles;
        document.querySelector(".bannerEbook").appendChild(styleSheet);
    };
    var styles = "@media (min-width: 768px) {" + ".bannerEbook {" + "content: url(https://img.selecoesbrasil.com.br/banner_ebook/banner_ebook_zc_LS_03-2023.png); max-width: 800px!important;}}";
}
英文:

Use either global functions (declared in the top scope)

function appendHtmlVB() {
    var divVB = document.getElementById(&quot;chamaBannerVB&quot;);
    divVB.innerHTML += &#39;&lt;a href=&quot;https://vidabalanceada.com.br/facaparte?mktcode=IDSTCS&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;bannerEbookVB&quot; src=&quot;https://img.selecoesbrasil.com.br/banner_ebook/mobile_banner_ebook_vb_LS_03-2023.png&quot; alt=&quot;Banner Mobile&quot; style=&quot;width: 100%; max-width: 480px;&quot;&gt;&lt;/a&gt;&#39;;
    var styleSheet = document.createElement(&quot;style&quot;)
    styleSheet.innerText = styles
    document.querySelector(&quot;.bannerEbookVB&quot;).appendChild(styleSheet);
}
function appendHtml() {
    var div = document.getElementById(&quot;chamaBannerZC&quot;);
    div.innerHTML += &#39;&lt;a href=&quot;https://zenconecta.com.br/facaparte?mktcode=IZSTCS&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;bannerEbook&quot; src=&quot;https://img.selecoesbrasil.com.br/banner_ebook/mobile_banner_ebook_zc_LS_03-2023.png&quot; alt=&quot;Banner Mobile&quot; style=&quot;width: 100%; max-width: 480px;&quot;&gt;&lt;/a&gt;&#39;;
    var styleSheet = document.createElement(&quot;style&quot;)
    styleSheet.innerText = styles
    document.querySelector(&quot;.bannerEbook&quot;).appendChild(styleSheet);
}

var element = document.getElementById(&#39;chamaBannerVB&#39;)
var elementZC = document.getElementById(&#39;chamaBannerZC&#39;)
if (element !== null) {
    window.onload = appendHtmlVB;
    var styles = &quot;@media (min-width: 768px) {&quot; + &quot;.bannerEbookVB {&quot; + &quot;content: url(https://img.selecoesbrasil.com.br/banner_ebook/banner_ebook_vb_LS_03-2023.png); max-width: 800px!important;&quot; +&quot;}}&quot;

} else {
    window.onload = appendHtml;
    var styles = &quot;@media (min-width: 768px) {&quot; + &quot;.bannerEbook {&quot; + &quot;content: url(https://img.selecoesbrasil.com.br/banner_ebook/banner_ebook_zc_LS_03-2023.png); max-width: 800px!important;&quot; +&quot;}}&quot;
}

or just don't use function declarations at all, directly assigning a function expression to the event handler attribute:

var element = document.getElementById(&#39;chamaBannerVB&#39;)
var elementZC = document.getElementById(&#39;chamaBannerZC&#39;)
if (element !== null) {
    window.onload = function appendHtmlVB() {
        var divVB = document.getElementById(&quot;chamaBannerVB&quot;);
        divVB.innerHTML += &#39;&lt;a href=&quot;https://vidabalanceada.com.br/facaparte?mktcode=IDSTCS&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;bannerEbookVB&quot; src=&quot;https://img.selecoesbrasil.com.br/banner_ebook/mobile_banner_ebook_vb_LS_03-2023.png&quot; alt=&quot;Banner Mobile&quot; style=&quot;width: 100%; max-width: 480px;&quot;&gt;&lt;/a&gt;&#39;;
        var styleSheet = document.createElement(&quot;style&quot;)
        styleSheet.innerText = styles
        document.querySelector(&quot;.bannerEbookVB&quot;).appendChild(styleSheet);
    };
    var styles = &quot;@media (min-width: 768px) {&quot; + &quot;.bannerEbookVB {&quot; + &quot;content: url(https://img.selecoesbrasil.com.br/banner_ebook/banner_ebook_vb_LS_03-2023.png); max-width: 800px!important;&quot; +&quot;}}&quot;

} else {
    window.onload = function appendHtml() {
        var div = document.getElementById(&quot;chamaBannerZC&quot;);
        div.innerHTML += &#39;&lt;a href=&quot;https://zenconecta.com.br/facaparte?mktcode=IZSTCS&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;bannerEbook&quot; src=&quot;https://img.selecoesbrasil.com.br/banner_ebook/mobile_banner_ebook_zc_LS_03-2023.png&quot; alt=&quot;Banner Mobile&quot; style=&quot;width: 100%; max-width: 480px;&quot;&gt;&lt;/a&gt;&#39;;
        var styleSheet = document.createElement(&quot;style&quot;)
        styleSheet.innerText = styles
        document.querySelector(&quot;.bannerEbook&quot;).appendChild(styleSheet);
    };
    var styles = &quot;@media (min-width: 768px) {&quot; + &quot;.bannerEbook {&quot; + &quot;content: url(https://img.selecoesbrasil.com.br/banner_ebook/banner_ebook_zc_LS_03-2023.png); max-width: 800px!important;&quot; +&quot;}}&quot;
}

huangapple
  • 本文由 发表于 2023年3月31日 21:44:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899253.html
  • ecmascript-5
  • ecmascript-6
  • google-tag-manager
  • html
  • javascript
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定