简体   繁体   English

在实现文档就绪功能时避免Javascript Function预期的错误

[英]Avoiding Javascript Function expected error on implementing document ready function

I have a component on my page that needs to be hidden until the documented is ready. 我的页面上有一个组件需要隐藏,直到准备好文档为止。 Therefore I have defined a couple of DIVs and implemented the document ready function: 因此,我定义了几个DIV并实现了文档准备功能:

<div id="LoadingDiv" style="display:block;text-align:center">
    <i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>
</div>
<div class="form-horizontal" id="LoadedDiv" style="display:none;">
   ...
</div>

<script  type="text/javascript"> 

$(document).ready(function () {
    setTimeout(function () {
        document.getElementById("LoadingDiv").style.display = "none";
        document.getElementById("LoadedDiv").style.display = "block";
    }, 500);
})

$(document).on("change", function () {
        window.onbeforeunload = function () {
            return "your changes to this form have not been saved";
        };
})
</script>

Whilst this seems to work, it generates 'Function Expected' error in the browser's developer console. 尽管这似乎可行,但它会在浏览器的开发人员控制台中生成“预期功能”错误。 I've tried various different ways of expressing the function - see below. 我尝试了多种表达功能的方法-参见下文。 I've also tried replacing the function body with something simple like alert("me"). 我还尝试过用Alert(“ me”)之类的简单东西替换函数主体。 The error goes away if I remove the 'ready' function, but keep the document 'change' function. 如果删除“就绪”功能,但保留文档“更改”功能,该错误就会消失。 Therefore I'm now quite sure the problem lies in the 'ready' function definition. 因此,我现在可以确定问题出在“就绪”函数定义中。

Questions: 问题:

  1. Why am I getting a Function expected error? 为什么会出现功能预期错误? The function seems to be properly formed and I'm setting properties rather using them as methods. 该函数似乎格式正确,我正在设置属性,而不是将它们用作方法。
  2. What is the correct convention for implementing a document ready function? 实现文档准备功能的正确约定是什么? Seemingly there are many approaches - eg $(document).on("ready", function () {...}; }). 似乎有很多方法-例如$(document).on(“ ready”,function(){...};})。 It would be great to agree on just one that works well and without error. 最好就一个运作良好且没有错误的协议达成共识。

By the way, I'm using JQuery 2.2.0 with Bootstrap 3.3.6. 顺便说一句,我使用的是带有Bootstrap 3.3.6的JQuery 2.2.0。

You forgot to add ; 你忘了加; at the end of function. 在功能结束时。 in both function. 在两个功能上。

$(document).ready(function () {
setTimeout(function () {
    document.getElementById("LoadingDiv").style.display = "none";
    document.getElementById("LoadedDiv").style.display = "block";
}, 500); 
}); //added ; at the end of function.

Try replacing this code with your code... 尝试将此代码替换为您的代码...

Well spotted Gaurang! 发现高良! I need new spectacles ;-) The correct code is 我需要新眼镜;-)正确的代码是

<div id="LoadingDiv" style="display:block;text-align:center">
  <i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>
</div>
<div class="form-horizontal" id="LoadedDiv" style="display:none;">
   ...
</div>

<script  type="text/javascript"> 

$(document).ready(function () {
   setTimeout(function () {
     document.getElementById("LoadingDiv").style.display = "none";
     document.getElementById("LoadedDiv").style.display = "block";
   }, 500);
}); 
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM