英文:
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('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;" +"}}"
}
<!-- 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("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;" +"}}"
}
or just don't use function declarations at all, directly assigning a function expression to the event handler attribute:
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;" +"}}"
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论