[英]Javascript error halts $document.ready()
I'm updating an existing website running on Expression Engine. 我正在更新在Expression Engine上运行的现有网站。 So far, I've stayed away from any code I didn't write or couldn't understand.
到目前为止,我远离任何我未编写或无法理解的代码。 I recently must have altered some bit of code someplace (helpful, I know) and now a block of JS I didn't write is causing an error that seems to bypass the document.ready() event.
最近,我必须在某处更改了一些代码(很有帮助,我知道),现在我未编写的JS块导致了一个似乎绕过document.ready()事件的错误。 The window.load() event however is still taking place.
但是window.load()事件仍在发生。
In the Chrome DevTools Console, the error "Uncought TypeError: Cannot call method 'replace' of UNDEFINED" points to the definition of a function "fixedEncodeURIComponent" pasted below. 在Chrome DevTools控制台中,错误“未预料到的TypeError:无法调用UNDEFINED的方法'replace'”指向下面粘贴的函数“ fixedEncodeURIComponent”的定义。
$("#MessageContainer.Counted").counter({
type: 'char',
goal: 250,
count: 'down'
}).change(function(){
var TEMP = fixedEncodeURIComponent($(this).val());
$("#Message").val(TEMP);
});
var TEMP = fixedEncodeURIComponent($("#MessageContainer.Test").val());
$("#Message").val(TEMP);
function fixedEncodeURIComponent (str) {
str=str.replace(/"/g, '');
return encodeURIComponent(str).replace(/[!'()*]/g, escape);
}
As I interpret the error, this function is being passed a variable that is not a string. 当我解释该错误时,正在向此函数传递不是字符串的变量。 I added an alert(str) to the function definition and the result was UNDEFINED as I expected.
我在函数定义中添加了一个alert(str),结果是未预期的。 The first of several unknowns for me is which call to the function 'fixedEncodeURIComponent' is being passed a bad variable.
对我来说,第一个未知数是哪个对“ fixedEncodeURIComponent”函数的调用传递了错误的变量。 I assume that it's the first call, but that's just a guess.
我认为这是第一个电话,但这只是一个猜测。 It so happens that this first call contains a syntax I have never encountered before.
碰巧,第一次调用包含了我以前从未遇到过的语法。 I don't know how to interpret what happens when $(this) is passed as a function argument.
我不知道如何解释将$(this)作为函数参数传递时会发生什么。
Any insights would be greatly appreciated. 任何见解将不胜感激。 Also, if there's more information you need please let me know.
另外,如果您需要更多信息,请告诉我。 The client's site is password protected but I can include any code you request.
客户的网站受密码保护,但是我可以包含您要求的任何代码。
Thank you. 谢谢。
I'm taking a guess that the });
我猜测
});
on line 3 is exiting a document.ready context. 第3行上的退出退出document.ready上下文。 If that's the case then your second call to
fixedEncodeURIComponent
may be getting called before the DOM is even loaded. 如果是这种情况,那么甚至在加载DOM之前,可能已调用了对
fixedEncodeURIComponent
的第二次调用。
Start by wrapping 从包装开始
var TEMP = fixedEncodeURIComponent($("#MessageContainer.Test").val()); $("#Message").val(TEMP);
in a 在一个
$(function() {
// code
});
block. 块。 If that doesn't work, check that #MessageContainer.Test actually matches an element.
如果这不起作用,请检查#MessageContainer.Test是否确实匹配元素。 Since this is code you inherited, the class name "Test" clues me in that the block in question might be a remnant of someone trying to debug an issue and maybe it should have been removed.
由于这是您继承的代码,因此类名“ Test”为我提供了线索,因为所讨论的块可能是某人试图调试问题的残余,也许应该将其删除了。
I suspect $("#MessageContainer.Test")
since it looks like its supposed to be an ID selector instead of what it actually is when jQUery parses it(which is an ID selector combined with a class selector). 我怀疑
$("#MessageContainer.Test")
因为它看起来应该是一个ID选择器,而不是jQUery解析它时(它是一个与类选择器组合在一起的ID选择器)时的实际选择。 $("MessageContainer\\\\.Test")
allows you to select an element with ID MessageContainer.Test $("MessageContainer\\\\.Test")
允许您选择ID为MessageContainer.Test的元素
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.