JavaScript 问题会破坏浏览器吗?

发布于 2024-09-06 14:01:05 字数 596 浏览 5 评论 0原文

我正在运行 Google Website Optimizer,它执行客户端 cookie 检查以仅显示内容一次。

然而,当它们被 cookie 时,我收到一个 javascript 错误,这似乎并不致命,但我读到 1 个 js 错误足以完全破坏 Internet Explorer。

有问题的代码行是

此行由 Google 直接提供,但我收到未定义的错误。现在,它运行在页面的最底部,所以最坏的情况是 GA 不运行。

但有什么危害呢?


编辑:我没有直接要求调试我的 JS,因为这个问题很深奥,并且可能是由于错误或错误的假设造成的。问题是忽视这个问题是否有危险。

我得到的错误是:“utmx_section 未定义”,即使提供相关函数的 JavaScript 已正确包含在内,并且它确实引发错误的页面和未引发错误的页面之间的唯一区别是在页面上运行一些额外的 JQuery(由于检查 cookie 的条件),它不会引发错误。

进一步编辑:通过仅在需要时(即,在未使用 cookie 的用户上)让 PHP 条件执行 utmx 行来解决该问题。

I'm running Google Website Optimizer, and its executing client-side cookie checks to show content just once.

However, when they're cookied, I get a javascript error that doesn't SEEM fatal, but I've read that 1 js error is enough to totally break Internet Explorer.

the line of code in question is
<script>utmx_section("Blah")</script>

This line is provided directly by Google, but I get an error that is not defined. Now, it runs at the very bottom of the page, so the worst case is that GA doesn't run.

But what are the hazards?


EDIT: I wasn't directly asking to debug my JS, because the problem is esoteric and it's possible its due to a mistake or an erroneous assumption. The question was whether it was hazardous to ignore the problem.

The error I get is: "utmx_section is not defined", even though the JavaScript that provides the function in question is properly included, and the only difference between the pages that it does throw an error on and those that it doesn't is that some extra JQuery is run (due to a conditional that checks for a cookie) on the pages that it doesn't throw an error.

FURTHER EDIT: Got around the problem by having a PHP conditional execute the utmx line only when it would be needed (ie, on un-cookied users).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迎风吟唱 2024-09-13 14:01:05

大多数用户都关闭了 JavaScript 通知,这样他们就不会看到错误。在您的情况下,该错误不会“破坏浏览器”;它真正中断的地方是对该函数的后续调用以及该脚本块内该调用之后的任何代码:

utmx_section("Blah");
// later:
utmx_section("Correct"); <-- will not fire

并且:

utmx_section("Blah"); 
var a = b; <-- will not fire
doStuff(); <-- will not fire

在 PHP 中修复此问题非常困难。只需添加一个条件即可。当查看全局变量是否存在时,您必须使用“window”:

if(window.utmx_section){
    utmx_section("Blah");
}

Most users have JavaScript notifications turned off so they wouldn't see the error. The error will not "break the browser" in your case; where it does break is subsequent calls to that function and any code after that call within that script block:

utmx_section("Blah");
// later:
utmx_section("Correct"); <-- will not fire

and:

utmx_section("Blah"); 
var a = b; <-- will not fire
doStuff(); <-- will not fire

Fixing this in PHP was doing it the hard way. Just add a conditional. And you have to use "window" when looking to see if a global variable exists:

if(window.utmx_section){
    utmx_section("Blah");
}
維他命╮ 2024-09-13 14:01:05

为了严格回答标题“JavaScript 问题会破坏浏览器吗?”中的问题,有些人会这样做。

请考虑以下事项:

<script type="text/javascript">
  function doSomething() {
     //some stuff here
  }      
  for (;;) {
    doSomething();
  }

</script>

并非所有浏览器都足够智能来检测此类问题。这会挂起你的浏览器,所以你必须杀死它的进程。

较新的浏览器会检测到它并显示一条通知,允许您终止浏览器中的脚本,但正如我所说,并非所有浏览器都足够智能。

至于问题的正文,不确定 的问题是什么。您能否提供更多详细信息?

To respond strictly to the question in the title "Do JavaScript problems break the browser?", some do.

Consider the following:

<script type="text/javascript">
  function doSomething() {
     //some stuff here
  }      
  for (;;) {
    doSomething();
  }

</script>

Not all browser are smart enough to detect this sort of issue. This will hang your browser so you have to go killer on its process.

Newer browsers detect it and display a notification allowing your to kill the script in the browser, but as I said, not all are smart enough.

As to the body of your question, not sure what the issue with <script>utmx_section("Blah")</script> is. Could you provide more details?

东北女汉子 2024-09-13 14:01:05

我不太清楚你所说的“当它们被饼干化时”是什么意思
然而,是的,一段 javascript 通常会导致进一步的 javascript 处理停止。

无论如何,您应该调试该问题。谷歌分析代码适用于大量网站,所以我猜你实际上在其他地方遇到了问题。无论是您复制代码、依赖项的方式,还是您的某些代码、依赖项都被过滤掉了。

I'm not exactly sure what you mean by "when they're cookied"
However, yes, a piece of javascript will generally cause further javascript processing to halt.

Regardless, you should debug the problem. The google analytics code works on a tremendous number of sites, so I'm guessing you actually have a problem elsewhere. Either in how you copied the code, dependencies, or in some of yours which is filtering down.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文