【问题标题】:IE9 reports missing semicolon that existsIE9 报告缺少存在的分号
【发布时间】:2012-07-13 16:46:54
【问题描述】:

我通过创建脚本标签并将 javascript 写入它的 .innerHTML 来动态加载一些 .js。

我收到此错误:

SCRIPT1004: Expected ';' 
develop, line 1487 character 24

这毫无意义......所以我猜它是在报告行号是正确还是错误(我至少很高兴它做到了这一点而没有失败......1487 接近我的代码结束)。

Firefox 通常会报告正确的行,但总是偏离 1 行。我以为 IE9 也会...

如何进行故障排除。我已经验证了代码通过了 jshint.com,这使得它期待 ; 更加奇怪。如果它是真的,Jshint 会发现它。

在 IE9 上几乎是盲目的。

这是代码:上面有 10 行,下面有 10 行。第 1487 行是这样注释的。

    /**
     *Publik
     */

    var publik = {};
    publik.initMenu = function( )
    {
        top_element = document.getElementById( 'top_new' ); 
        bottom_element = document.getElementById( 'wrap_drop_down_new' );
        top_element.addEventListener( "mouseout", mouse_out, false ); 
        top_element.addEventListener( "mouseover", top_mouse_over, false ); // Line 1487
        bottom_element.addEventListener( "mouseout", mouse_out, false ); 
        bottom_element.addEventListener( "mouseover", bottom_mouse_over, false ); 
    };
    return publik;

}());
/*  Use this to create Event on completion of .js and remove cStart().

    var event_load_js = document.createEvent("HTMLEvents");
    event_load_js.initEvent( "blur", true, false );

每个 Beat 请求包含 top_mouse_over 的代码

/**
 *MMenu
 */

var MMenu = ( function () 
{
    /**
     *Private
     */

    var top_element,
        bottom_element,
        time_out_id = 0,
        TIME_DELAY = 1000;

    function showBottom()
    {
        top_element.style.border = '1px solid #cfcaca';
        top_element.style.borderBottom = '3px solid #cfcaca';
        bottom_element.style.visibility = 'visible';
    }
    function hideBottom()
    {
        top_element.style.border = '1px solid #faf7f7';
        bottom_element.style.visibility = 'hidden';
    }
    function top_mouse_over()
    {
        window.clearTimeout( time_out_id );
        showBottom();
    }
    function bottom_mouse_over()
    {
        window.clearTimeout( time_out_id );
    }
    function mouse_out()
    {
        time_out_id = window.setTimeout( hideBottom, TIME_DELAY );
    }

【问题讨论】:

  • 能展示top_mouse_over函数吗?
  • 我不认为您在生成代码时采用了正确的方法。你能显示你正在动态添加的相关代码吗?
  • @HiroProtagonist 对我来说也不错,我只是不确定 IE 在事件处理程序中将其作为事件处理程序行上的错误引用时是否真的会报告函数中的分号错误,但似乎并非如此。您是否尝试过注释掉这些行以查看错误是否消失?也许它会在另一个地方重新出现,这可以给你一个线索。
  • 这有帮助吗:stackoverflow.com/a/9246081/1220302。您的动态代码中可能有 uni-code 字符或拼写错误的“函数”吗?

标签: javascript internet-explorer-9


【解决方案1】:

从浏览器中复制您动态生成的代码并将其粘贴到 Notepad++ 中并搜索“?”看看你是否有任何 unicode 字符。我很确定控制台没有说实话。我以前也遇到过。

见:https://stackoverflow.com/a/9246128/1220302

【讨论】:

  • @hiro-protagonist - 对于 Notepad++,您无需重新配置即可查看 Unicode 字符。也很滑。祝你好运!
  • 我可以对 [\x00-\x7F] 进行正则表达式检查吗?
  • 在我的例子中,一位开发人员使用 Mac 并在本地部署到其 Web 服务器,而 Windows Phone 报告错误:SCRIPT1004。解决方案是使用不同的编码保存文件(UTF 是我们的第一个测试),它正确加载了 JS。
【解决方案2】:

可能与 Unicode 无关。可能是属性 onclick 或其他内联脚本中缺少引号。我刚碰到这个。示例:

<button onclick="alert('hi') title="hello">Click me</button>

此错误仅在解析 js 时发生,并在第 1 行给出错误。我发现这仅在 IE 中引发脚本解析错误,而不在 Chrome 中。错误是:

SCRIPT1004: Expected ';' 
mypage.html, line 1 character 54

因为我使用 jquery.html() 将此 HTML 注入到 DIV 中,所以它没有在页面加载时解析它,并且仅在某些条件下 - 但实际上这是一个脚本解析错误。

【讨论】:

    【解决方案3】:

    最近我在 IE 11 上也遇到了同样的问题,所以我做了以下事情来解决这个问题。

    1. 在 IE 浏览器中按“F12”打开控制台窗口
    2. 请在Emulation选项卡的文档模式或右侧顶部将IE版本改为11而不是旧版本(李>
    3. 当我们更改 IE 版本时,浏览器会自动重新加载

    【讨论】:

      猜你喜欢
      • 2011-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-05
      • 2015-10-28
      • 2018-07-26
      • 2019-05-18
      • 1970-01-01
      相关资源
      最近更新 更多