JavaScript 运行时错误:对象不支持属性或方法“jqGrid”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22447949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:11:20  来源:igfitidea点击:

JavaScript runtime error: Object doesn't support property or method 'jqGrid'

javascriptasp.netjqgrid

提问by AbSharp

I get the exception - "JavaScript runtime error: Object doesn't support property or method 'jqGrid'"when I move below block from maincontent of content page to header section of master page.

当我将下面的块从内容页面的主要内容移动到母版页的标题部分时,出现异常 - “JavaScript 运行时错误:对象不支持属性或方法 'jqGrid'”

<link href="../Content/jquery.jqGrid/ui.jqgrid.css" rel="stylesheet" />
<link href="../Content/jquery.jqGrid/jquery-ui-custom.css" rel="stylesheet" />
<script src="../Scripts/jquery-1.9.1.min.js"></script>
<script src="../Scripts/jquery.jqGrid.js"></script>
<script src="../Scripts/i18n/grid.locale-en.js"></script>



It only works when I place this block in trailing section of master page.
Could someone help me understand why it works only when I place this block in trailing section of master page but not in header section? I think this has something to do with loading dependencies and initialization of jqGrid, however I need concrete understanding.

它仅在我将此块放置在母版页的尾随部分时才有效。
有人能帮我理解为什么只有当我将此块放在母版页的尾随部分而不是标题部分时它才有效吗?我认为这与加载依赖项和jqGrid的初始化有关,但是我需要具体了解。

回答by Sai Avinash

You have to wait until DOM finishes loading. Then you can apply your scripts. Since, in your master page, you are using the script at the bottom, since the parser uses top downapproach, it executes the script, after the body is completed loading.

你必须等到DOM finishes loading. 然后你可以应用你的脚本。因为,在您的母版页中,您在底部使用脚本,因为解析器使用自上而下的方法,它会在正文加载完成后执行脚本。

So, if you do want to put in head section, ensure that all your script is inside document.ready()function

因此,如果您确实想放入 head 部分,请确保所有脚本都在document.ready()函数中

Also , know that it is always a best practiceto put all your scripts at bottomrather than at the header.

此外,要知道,它始终是一个最好的做法把所有的脚本,在bottom而不是在header

This is a good performance indicator. Also it avoidsthe usage of readyfunction

这是一个很好的性能指标。也是函数avoids的用法ready

Hope this helps..

希望这可以帮助..