java - Htmlunit ScriptException "console" is not defined - Stack Overflow

I am using htmlunit 2.9 and on java script parsing I am getting script exception due to console in foll

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/mypage/ll.html from (154, 36) to (301, 14)#168)]
.gargoylesoftware.htmlunit.ScriptException: ReferenceError: "console" is not defined. (script in http://localhost:8080/mypage/ll.html from (154, 36) to (301, 14)#168)
         at .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 .gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:545)
         at .gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:520)
         at .gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunctionIfPossible(HtmlPage.java:896)
         at .gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeEventHandler(EventListenersContainer.java:195)
         at .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?

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./mypage/ll.html from (154, 36) to (301, 14)#168)]
.gargoylesoftware.htmlunit.ScriptException: ReferenceError: "console" is not defined. (script in http://localhost:8080./mypage/ll.html from (154, 36) to (301, 14)#168)
         at .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 .gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:545)
         at .gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:520)
         at .gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunctionIfPossible(HtmlPage.java:896)
         at .gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeEventHandler(EventListenersContainer.java:195)
         at .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?

Share Improve this question edited Dec 31, 2011 at 21:39 Mike Samuel 121k30 gold badges227 silver badges254 bronze badges asked Dec 31, 2011 at 21:26 RehmanRehman 3,9986 gold badges30 silver badges29 bronze badges 2
  • Does it also stop if you use BrowserVersion.FIREFOX_3_6? – milan Commented Dec 31, 2011 at 22:00
  • Possibly helpful (see lower answers as well): stackoverflow./questions/1215392/… – Jared Farrish Commented Dec 31, 2011 at 22:37
Add a ment  | 

3 Answers 3

Reset to default 2

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.

https://sourceforge/tracker/index.php?func=detail&aid=3518475&group_id=47038&atid=448266 implies that support for console was just added to HtmlUnit.

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745485247a4629736.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信