[英]Object Expected error in js file
i have a control which is on update panel. 我有一个在更新面板上的控件。 i want my javascript code run every time the updatePAnel is updated.
我希望每次updatePAnel更新时都运行我的JavaScript代码。 i used something like this:
我用这样的东西:
ScriptManager.RegisterStartupScript(this, GetType(), "my_script", "runFunction();", true);
Everything worked fine. 一切正常。 Then i changed my code a bit in order to use js file with js code:
然后,为了将js文件与js代码一起使用,我做了一些更改:
ScriptManager.RegisterClientScriptInclude(this, GetType(), "my_script", "~\Scripts\MyScript.js");
But now i get Object Expected error in MyScript.js. 但是现在我在MyScript.js中遇到了Object Expected错误。
what can be wrong? 有什么问题吗?
EDIT: 编辑:
my js code: 我的js代码:
var http = getHTTPObject(); // We create the HTTP Object
function checkFilesExists() {
var links = $('.PrIcon').find('a');
for (i = 0; i < links.length; i++) {
if (!checkFileExists(links[i].href)) {
links[i].parentNode.parentNode.style.visibility = 'hidden';
}
}
}
checkFilesExists();
function checkFileExists(handleRequest) {
/*http.onreadystatechange = handleHttpReceiveNewPwd;*/
/*http.setRequestHeader('Content-Type', 'application/pdf');*/
try {
http.open('HEAD', handleRequest, false);
http.send(null);
return http.status == 200;
} catch (e) {
return false;
}
}
function getHTTPObject() {
if (window.XMLHttpRequest) { // Mozilla, Safari,...
var obj = new XMLHttpRequest();
if (obj.overrideMimeType) {
obj.overrideMimeType('application/pdf');
}
}
else if (window.ActiveXObject) { // IE
try {
var obj = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
var obj = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) { }
}
}
if (obj)
return obj;
}
the error occurs on this line: var links = $('.PrIcon').find('a'); 该行发生错误:var links = $('。PrIcon')。find('a');
Your script seems to depends on some other (probably jQuery), but the ScriptManager.RegisterClientScriptInclude
renders the <script>
tag too early in the page (before the script your's depends on is interpreted, and thus, not creating the necessary objects). 您的脚本似乎依赖于其他脚本(可能是jQuery),但是
ScriptManager.RegisterClientScriptInclude
在页面中太早呈现了<script>
标记(在解释您所依赖的脚本之前,因此没有创建必要的对象)。 A better option in this case is to use the ScriptManager.RegisterStartupScript
method, but instead of passing the script body, you shall pass the entire <script>
tag with your script's address: 在这种情况下,一个更好的选择是使用
ScriptManager.RegisterStartupScript
方法,但是除了传递脚本主体之外,还应传递整个<script>
标记以及脚本的地址:
ScriptManager.RegisterStartupScript(updatePanel, updatePanel.GetType(), "a_key", "<script type='text/javascript' src='my_script.js'></script>", false);
Note that the last parameter, which sets the addScriptTags
flag is set to false, allowing you to render the entire tag with the src
attribute defined. 请注意,将
addScriptTags
标志设置的最后一个参数设置为false,从而允许您使用定义的src
属性呈现整个标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.