问题
This is my tag:
<script>
window.intercomSettings = {
app_id: "fanwstw2"
};
</script>
<script>
(function() {
var w = window;
var ic = w.Intercom;
if (typeof ic === "function") {
ic('reattach_activator');
ic('update', intercomSettings);
} else {
var d = document;
var i = function() {
i.c(arguments)
};
i.q = [];
i.c = function(args) {
i.q.push(args)
};
w.Intercom = i;
function l() {
var s = d.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = 'https://widget.intercom.io/widget/fanwstw2';
var x = d.getElementsByTagName('script')[0];
x.parentNode.insertBefore(s, x);
}
if (w.attachEvent) {
w.attachEvent('onload', l);
} else {
w.addEventListener('load', l, false);
}
}
})()
</script>
This is the error message
回答1:
I had this issue as well, and I believe it's because GTM, which is based on the ES5 engine, is seeing ES6 code and trying to parse it as ES5. It is likely coming from your l()
function declaration within the if
block. Try to move that out of the if
block, like just before it and compile the tag again, like this:
(function() {
var w = window;
var ic = w.Intercom;
// moved this out of if block
function l() {
var s = d.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = 'https://widget.intercom.io/widget/fanwstw2';
var x = d.getElementsByTagName('script')[0];
x.parentNode.insertBefore(s, x);
}
if (typeof ic === "function") {
ic('reattach_activator');
ic('update', intercomSettings);
} else {
var d = document;
var i = function() {
i.c(arguments)
};
i.q = [];
i.c = function(args) {
i.q.push(args)
};
w.Intercom = i;
if (w.attachEvent) {
w.attachEvent('onload', l);
} else {
w.addEventListener('load', l, false);
}
}
})()
回答2:
I can't see any issue with your code, maybe it's the parser being overly awkward. It could also be caused by you wrapping within an IIFE.
How about changing this line below (just to rule it out being weird)..
ic('update', intercomSettings);
To this...
ic('update', w.intercomSettings);
来源:https://stackoverflow.com/questions/48440623/why-am-i-getting-a-javascript-compiler-error-when-trying-to-publish-changes-to-m