【问题标题】:ReferenceError in js console -SCRIPT5009: 'channel_click' is undefinedjs控制台中的ReferenceError -SCRIPT5009:'channel_click'未定义
【发布时间】:2013-01-25 10:53:14
【问题描述】:

我在 js 控制台中的 IE9 中收到“SCRIPT5009: 'channel_click' is undefined”错误。该代码在 chrome 和 firefox 中运行,但在单击链接以在 IE9 中启动 js 序列时突然停止。

我一直试图解决这个问题,但没有成功,但我所做的是定时发生一系列事件:

1) onclick - toggle div to hide
2) wait 1500 
3) open print command
- User closes the window to return to page
4) wait 3500 toggle div to show again

我这样做的原因是我不希望打印预览在用户决定打印页面时拾取这些系列的 div。

Java脚本:

<script>
//WAITS UNTIL EXCESS IS HIDDEN THEN FIRES PRINT COMMAND 
function channel_click(){

// anon wrapper function, 2 second delay
setTimeout( function () {
window.print();return false;
} , 1500 );

}
</script>


<script>
//HIDES EXCESS IN PREP FOR FIRING PRINT COMMAND PREVIOUS FUNCTION EXECUTES IN BETWEEN FOLLOWING FUNCTIONS
$(".hideshow").click(function () {
$("#header,#footer").toggle("slow");

setTimeout( function () {
$("#header,#footer").toggle("slow");
} , 3500 );
$('.modal-backdrop').hide();
});
</script>

HTML:

<a href="#" role="button" class="hideshow" onclick="channel_click()">Print Preview</a>

<div id="header">
    <div class="pull-left">
    <h1>Edt Mode</h1>
    <h6 style="padding-bottom: 30px;">Some informational text here</h6>
</div>

<div>
    <div class="pull-left">
    <h1>PRINT ME!</h1>
    <h6 style="padding-bottom: 30px;">PRINT ME -  PRINT ME - PRINT ME</h6>
</div>

<div id="footer">
    <div class="pull-left">
    <h1>Edt Mode</h1>
    <h6 style="padding-bottom: 30px;">Some informational text here</h6>
</div>

<div class="model-backdrop">wow this is some more text</div>

【问题讨论】:

  • 实际上你的代码看起来不错,你只是缺少&lt;script type="text/javascript"&gt;。这有帮助吗?您可能还需要将脚本包装到 $(document).ready(/**/);
  • 现代浏览器(因为他使用 chrome)它不需要 type="text/javascript"
  • @Sandeep 建议您使用它,有些引擎还会渲染其他脚本类型,因此浏览器不会因为尝试而失败。
  • 是的,我同意你的观点,我总是使用它。但在这种情况下,这不会是问题。我已经在这里发布并回答了小提琴。

标签: javascript jquery html


【解决方案1】:

在您尝试在 html 中使用它之前,您会加载您的 javascript 文件吗? 如果你这样做,那不是问题,但如果你不尝试链接到 html 文件末尾的 .js 文件。

如果您不希望这很重要,您必须将您的 channel_click 包装在 window.onload 中,或者如果您使用 jQuery

$(document).ready(function() {
    function channel_click() {
        ....
    }
})

【讨论】:

    【解决方案2】:

    尝试从您的锚标记中删除 onclick:

    <a href="#" role="button" class="hideshow">Print Preview</a>
    

    JS:

    <script type="text/javascript">
    function channel_click(){    
        // anon wrapper function, 2 second delay
        setTimeout( function () {
             window.print();return false;
         } , 1500 );    
     }
    
        $(document).ready(function() {  //add this
            $(".hideshow").click(function (e) {
                e.preventDefault();
                $("#header,#footer").toggle("slow");
                channel_click(); //call print
    
               setTimeout( function () {
                  $("#header,#footer").toggle("slow");
               } , 3500 );
               $('.modal-backdrop').hide();
             });
        });
    

    【讨论】:

      【解决方案3】:

      它正在按你的预期工作

      http://jsbin.com/imimom/2

      【讨论】:

        【解决方案4】:

        由于您的 html 格式不正确,我看不出有什么问题。也许您可以向我们提供完整的标记?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-08
          • 1970-01-01
          • 1970-01-01
          • 2021-04-24
          • 1970-01-01
          相关资源
          最近更新 更多