开发者

Script tags in body do not get evaluated after previous JavaScript errors

开发者 https://www.devze.com 2023-02-15 06:25 出处:网络
I have one HTML file where a JavaScript method (say) postIt() is defined inside the <script> tag. Its tag is preceded by two references to external JavaScript files which no longer exist. All th

I have one HTML file where a JavaScript method (say) postIt() is defined inside the <script> tag. Its tag is preceded by two references to external JavaScript files which no longer exist. All these <script> tags are inside <body>.

I had to set WebClient.setThrowExceptionOnFailingStatusCode to false, so that I can proceed with the non-existent JavaScript files.

But when I try calling page.executeJavascript("postIt()"), I get Reference not found error!

  1. What is the reason for this and do we have any possible workaround?
  2. Does the JavaScript engine run in its o开发者_JAVA百科wn thread?


Most JavaScript engines stop parsing/evaluating code when they encounter parsetime/compiletime errors, just like other compilers.

This is a feature, not a bug.

The workaround is to fix the errors.


Well, as it turns out, I was invoking incorrect JavaScript code which in turn redirected my code to a 404 page. But since I had set webClient.setThrowExceptionOnFailingStatusCode() and webClient.setPrintContentOnFailingStatusCode() to false, HtmlUnit didn't notify me about this.

So for these kind of issues we need to keep the above flags turned on, and remove ALL JavaScript errors.

0

精彩评论

暂无评论...
验证码 换一张
取 消