ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-Htmlunit ScriptException“控制台”未定义

2019-11-01 22:36:09  阅读:370  来源: 互联网

标签:htmlunit javascript java rhino


我正在使用htmlunit 2.9并在Java脚本解析中由于以下异常中的控制台而导致脚本异常

function debug(o){
  if (console && console.log){
    console.log(o)
  }
};

堆栈跟踪

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中的控制台吗?

解决方法:

您的if条件结构不正确:

if (console && console.log){

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

尝试:

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

要么:

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

由于Firefox和Chrome和Safari一样实现了Firebug API,因此在Firefox中不会引发错误.但是,默认情况下,Internet Explorer不会,因此,这里值得进行适当的功能检查,因为它会在未实现此API的浏览器中引发ReferenceError.

标签:htmlunit,javascript,java,rhino
来源: https://codeday.me/bug/20191101/1986842.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有