如果调试器关闭,Internet Explorer 会发出 运行 代码

Internet Explorer issues running code if debugger closed

昨天我在使用 Internet Explorer 时遇到了一个有趣的问题。脚本 运行 在 Chrome、Firefox、Safari 上完美运行,但在 Internet Explorer 11 上它不会执行任何操作。如果我打开调试器,它 运行 很顺利并且一切正常,但是当我关闭调试器时它停止工作,我不知道为什么会这样。我的第一个想法是 IE 扩展,但我毫不掩饰地禁用了它们。我尝试 运行 在安全模式下使用管理员权限,但似乎没有任何效果。

总结一切:IE - 脚本 运行s 仅当调试器打开时。没有产生错误,只是不起作用。

如果有任何关于我能做些什么的想法,我将非常高兴。提前谢谢你。

----------------编辑-------------- 这是没有 运行.

的脚本
        for (var i = 0; i < AllStrategyGrids.length; i++) {
                try {
                    isChange = true;
                    var data = $("#objectives").data("kendoGrid").select().data();
                    if (AllStrategyGrids[i].ID == data.uid) {
                        var jsonData = new Object();
                        jsonData.StrategicID = "1";
                        jsonData.ObjectiveID = $("#ObjectiveID").val();
                        jsonData.HeaderID = "00000000-0000-0000-0000-000000000000";
                        jsonData.PeriodID = "00000000-0000-0000-0000-000000000000";
                        jsonData.Strategic = "Please enter strategic";
                        jsonData.TaskStatus = "";
                        jsonData.TaskStatusID = "1";
                        jsonData.Position = "";
                        jsonData.Sorted = "1";
                        jsonData.SessionID = "00000000-0000-0000-0000-000000000000";
                        tmpGrid = AllStrategyGrids[i].Grid.data("kendoGrid");
                        var dataRows = tmpGrid.items();
                        var rowIndex = dataRows.index(tmpGrid.select());
                        $.ajax({
                            url: "CreateStrategy",
                            type: 'POST',
                            data:
                                    {
                                        strategics: jsonData,
                                        VersionID: $("#VersionUID").val(),
                                        index: rowIndex
                                    },
                            success: function () {
                                tmpGrid.dataSource.read();
                            }
                        });
                    }
                } catch (e) { }
            }

只是猜测,这可能是因为您在该脚本中有一个 console.log,而在 IE 中,如果您的调试器关闭,则控制台对象不存在...我们都在那里 :)

一个简单的解决方法是尽可能早地在您的站点中添加小填充程序。它不会在除 IE 之外的任何浏览器上执行任何操作,只会停止可能阻止您的其他 JS 的执行错误来自 运行...

的代码
   <script>
      if(!console)console={log:function(){}};
   </script>

这里有一个更强大的解决方案:

'console' is undefined error for Internet Explorer

---- 编辑

好的,我可以从您的代码中看到一件事,即您将无声地失败,因为您使用了 try catch 但未对其执行任何操作。这将捕获您遇到的任何异常(阻止它到达您的 window 从而使您看起来没有错误)。我可能至少会在测试时提醒错误消息(这样你就不需要打开调试器)并查看是否抛出了任何东西...

我会怀疑你的 ajax 请求自己..那个或 IE8 中的未定义..所以添加一些日志记录警报(我知道蛮力)来测试你在某些点的假设,例如

alert("Reach here and data="+data);

或者,我还可以看到您的 ajax 请求没有失败的回调,这可能是添加到您的调用中的好主意。可能是由于某种原因没有成功...

                    $.ajax({
                            url: "CreateStrategy",
                            type: 'POST',
                            data:
                                    {
                                        strategics: jsonData,
                                        VersionID: $("#VersionUID").val(),
                                        index: rowIndex
                                    },
                            success: function () {
                                tmpGrid.dataSource.read();
                            }
                        })
                        .fail(function() {
                           alert( "error" );
                           //handle a failed load gracefully here
                        })
                        .always(function() {
                           alert( "complete" );
                           //useful for any clean up code here
                        });

最后的思考。

由于您正在检查 DOM 的项目,我不知道何时调用此代码,但以防万一它在页面重新加载后直接调用..让我们假设 IE 中的某些东西是在 'that' 点还没有准备好,可能是 DOM 还没有准备好被查询?尝试在 DOM 准备就绪时执行此代码。有很多方法可以实现此 $.ready , setTimeout(f(){},1) 或香草......但你明白了..

注意: 使用开发人员工具调试脚本:MSDN

To enable script debugging for all instances of Internet Explorer, on the Internet Options menu, click the Advanced tab. Then, under the Browsing category, uncheck the Disable script debugging (Internet Explorer) option, and then click OK. For the changes to take effect, close all instances of Internet Explorer then reopen them again.