Ins*_*iak 2 javascript bing tracking-pixel
I'm struggling to understand why this javascript error keeps popping up on my website and I can't reproduce it. I've followed the bing documentation to install their tracking pixel so I have this in my header :
<script>
(function(w,d,t,r,u){var f,n,i;w[u]=w[u]||[],f=function(){
var o={ti:"xxxxxxxx"};o.q=w[u],w[u]=new UET(o),
w[u].push("pageLoad")},n=d.createElement(t),n.src=r, n.async=1,
n.onload=n.onreadystatechange=function(){var s=this.readyState;
s&&s!=="loaded"&&s!=="complete"||(f(),n.onload=n.onreadystatechange=null)},
i=d.getElementsByTagName(t)[0],i.parentNode.insertBefore(n,i)})
(window,document,"script","//bat.bing.com/bat.js","uetq");
</script>
Run Code Online (Sandbox Code Playgroud)
However in some browsers, the UET variable in this code, which is loaded by the url given as the r variable, seems to not be defined while it's only called when the url is loaded...
If anybody can make sense of it before I reach out to bing, I'll be very thankful!
小智 6
基于已接受的答案,我相信可能有一种方法可以防止这些错误。
w[u]=new UET(o)你看到剧本里的那句话了吗?我们知道这w意味着window那里。UET 是在全局范围(即全局window对象)上创建的对象,因此可以测试它的存在window.UET(尝试window.UET在加载脚本和不加载脚本的情况下在控制台上运行,您将看到前一种情况返回undefined ,后一种情况返回case 返回一个 function function UET(o) {...}。因此,从技术上讲,您可以更改脚本,在其中添加条件,如下所示:
if(w.UET){w[u]=new w.UET(o)}
Run Code Online (Sandbox Code Playgroud)
下面是我尝试解构相对神秘的寻找<script>更具可读性的函数调用,以便更好地了解那里发生的事情(如果您像我一样在 SPA 中使用它,这也可能会很方便):
function initMicrosoftUET(
w,
d,
t = "script",
r = "//bat.bing.com/bat.js",
u = "uetq"
) {
var f, n, i;
w[u] = w[u] || [];
f = function () {
var o = { ti: "xxxxxxxx" };
o.q = w[u];
if (w.UET) w[u] = new w.UET(o) || [];
w[u].push("pageLoad");
}
n = d.createElement(t);
n.src = r;
n.async = 1;
n.onload = n.onreadystatechange = function () {
var s = this.readyState;
(s && s !== "loaded" && s !== "complete") || f();
n.onload = n.onreadystatechange = null;
}
i = d.getElementsByTagName(t)[0];
i.parentNode.insertBefore(n, i);
};
initMicrosoftUET(window, document);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1212 次 |
| 最近记录: |