Htmlunit ScriptException“控制台”没有定义

发布于 2024-12-23 09:42:21 字数 1890 浏览 4 评论 0原文

中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月苍老的讽刺 2024-12-30 09:42:21

您的 if 条件结构不正确:

if (console && console.log){

如果未设置,第一个 if 将会抛出错误;在未定义的环境中访问控制台就像访问任何未定义的变量一样;它会抛出一个ReferenceError

尝试:

if( typeof console != "undefined" && console.log ) {

或者:

if(window.console && console.log) {

它不会在 Firefox 中抛出错误,因为 Firefox 实现了 Firebug API,Chrome 和 Safari 也是如此。但是,默认情况下,Internet Explorer 不会,因此,值得在这里进行适当的功能检查,因为它会在未实现此 API 的浏览器中抛出 ReferenceError。

Your if condition isn't properly structured:

if (console && console.log){

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 a ReferenceError.

Try:

if( typeof console != "undefined" && console.log ) {

Or:

if(window.console && console.log) {

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.

愁以何悠 2024-12-30 09:42:21

您的代码确实使用了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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文