昨天在工作中遇到几个紧急的Bug,其中之一就是WebView中与js交互的一个问题。
先把这行代码贴上来给大家看看:
document.body.insertBefore(rightPanel);
这行代码是为了给Html中增加一个div写的,奇怪的是这行代码在Android5.0以下的版本中跑起来居然没问题,在Android5.0以上的版本中,这段代码是不成功的。
Android Logcat中的异常信息如下:
08-19 15:45:18.875: I/chromium(26394): [INFO:CONSOLE(1)] "Uncaught NotFoundError: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node.", source: (1)
ok究其缘由:
不难看出,insertBefore这个方法是有两个参数的,所以项目中的代码其实是错误的,js是有问题的,难怪5.0以上的设备中有问题。
解决方案:
我用了另一种方法替换解决之:
document.body.appendChild(rightPanel);
There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.