Htmlunit ScriptException“控制台”没有定义
中的 console
,我收到脚本异常,
function debug(o){
if (console && console.log){
console.log(o)
}
};
我正在使用 htmlunit 2.9 并在 java 脚本解析中,由于以下异常Stacktrace
EcmaError:
lineNumber=[168]
column=[0]
lineSource=[null]
name=[ReferenceError]
sourceName=[script in http://localhost:808/mypage/ll.html from (154, 36) to (301, 14)]
message=[ReferenceError: "console" is not defined. (script in http://localhost:8080.com/mypage/ll.html from (154, 36) to (301, 14)#168)]
com.gargoylesoftware.htmlunit.ScriptException: ReferenceError: "console" is not defined. (script in http://localhost:8080.com/mypage/ll.html from (154, 36) to (301, 14)#168)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:595)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:537)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:538)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:545)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:520)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunctionIfPossible(HtmlPage.java:896)
at com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeEventHandler(EventListenersContainer.java:195)
at com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeBubblingListeners(EventListenersContainer.java:214)
如果我在 Firefox 上尝试指定页面,它工作正常,我已经尝试过v 3.6 以及 9.0.1。
我还尝试设置 setThrowExceptionOnScriptError(false)
以避免异常,但引擎在收到错误后停止或不解析 javascript。
javascript引擎有什么办法可以理解javascript中的console
吗?
I am using htmlunit 2.9 and on java script parsing I am getting script exception due to console
in following exception
function debug(o){
if (console && console.log){
console.log(o)
}
};
Stacktrace
EcmaError:
lineNumber=[168]
column=[0]
lineSource=[null]
name=[ReferenceError]
sourceName=[script in http://localhost:808/mypage/ll.html from (154, 36) to (301, 14)]
message=[ReferenceError: "console" is not defined. (script in http://localhost:8080.com/mypage/ll.html from (154, 36) to (301, 14)#168)]
com.gargoylesoftware.htmlunit.ScriptException: ReferenceError: "console" is not defined. (script in http://localhost:8080.com/mypage/ll.html from (154, 36) to (301, 14)#168)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:595)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:537)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:538)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:545)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:520)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunctionIfPossible(HtmlPage.java:896)
at com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeEventHandler(EventListenersContainer.java:195)
at com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeBubblingListeners(EventListenersContainer.java:214)
if I try specified page on firefox it works fine, I have tried v 3.6 as well as 9.0.1.
i have tried also to set setThrowExceptionOnScriptError(false)
in order to avoid exception but engine stops or do not parse javascript after getting an error.
Is there any way that javascript engine can understand console
in javascript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
if
条件结构不正确:如果未设置,第一个 if 将会抛出错误;在未定义的环境中访问控制台就像访问任何未定义的变量一样;它会抛出一个
ReferenceError
。尝试:
或者:
它不会在 Firefox 中抛出错误,因为 Firefox 实现了 Firebug API,Chrome 和 Safari 也是如此。但是,默认情况下,Internet Explorer 不会,因此,值得在这里进行适当的功能检查,因为它会在未实现此 API 的浏览器中抛出 ReferenceError。
Your
if
condition isn't properly structured:That first if will throw an error if its not set; accessing
console
in environments its not defined in is like accessing any undefined variable; it will throw aReferenceError
.Try:
Or:
It doesn't throw in error in Firefox since Firefox implements the Firebug API, as do Chrome and Safari. But, by default, Internet Explorer does not, so, it's worth doing a proper feature check here, as it will throw a ReferenceError in browsers that don't implement this API.
https://sourceforge.net/tracker/index.php?func=detail&aid=3518475&group_id=47038&atid=448266 表示支持
console
刚刚添加到 HtmlUnit。https://sourceforge.net/tracker/index.php?func=detail&aid=3518475&group_id=47038&atid=448266 implies that support for
console
was just added to HtmlUnit.您的代码确实使用了java脚本控制台对象,并且直到当前版本才支持它,并且承诺在下一个版本中支持它,正如所说的此处
Your code does use the java script console object, and it's not supported till the current version, and it is promised to be supported in the next release as it is said here