sammy.js让处理程序隐藏错误

时间:2013-03-20 10:30:49

标签: javascript error-handling sammy.js

使用 sammy.js 版本 0.7.4 (也是 0.7.1 )的库时,发现如果在某些错误发生期间发生了get处理函数的执行,没有任何内容打印到控制台。

例如,在以下代码片段中,虽然不存在名称为notExistingFunction的函数,但不会向控制台打印任何内容:

<html>
    <head>
        ...
        <script src="/path/to/jquery-1.5.2.min.js"></script>
        <script src="/path/to/sammy-0.7.4.min.js"></script>
        ...
    </head>
    <body>
        <script>
            $(document).ready(function() {
                var sammy = Sammy(function() {
                    this.get('#someAnchor', function() {
                        // this function doesn't exist
                        notExistingFunction();
                    });
                });
                sammy.run();

                // this should lead to execution of above handler
                window.location.hash = '#someAnchor'; 
            });
        </script>
        ...
    </body>
</html>

这确实使页面的故障排除变得复杂,有人也经历过这种情况吗?这是预期的行为还是一个错误?有没有解决方法呢?

提前多多感谢

1 个答案:

答案 0 :(得分:1)

原来比我想象的要容易 - 在检查了 sammy.js 的来源之后,发现默认情况下raise_errors标志被设置为false并管理错误报告。

因此,将代码上方的部分修改为:

<html>
    <head>...</head>
    <body>
        <script>
            ...
            sammy.raise_errors = true;
            sammy.run();
            ...
        </script>
    </body>
</html>

开始显示不错的错误。