简体   繁体   English

Android Webview:控制台未定义

[英]Android Webview: console is not defined

I'm trying to open a html with some javascripts in a Android Webview and getting the following error after implements onConsoleMessage of my WebChromeClient: 我正在尝试在Android Webview中打开带有某些javascript的html,并在实现我的WebChromeClient的onConsoleMessage后收到以下错误:

Uncaught ReferenceError: console is not defined -- From line 10 of file:///storage/sdcard0/TargetApp/e184bae3-5824-4e23-a26e-820ce6d32aa2/pres/fce4da510de8431bB3eeD5bdbd1c695d/fce4da510de8431bB3eeD5bdbd1c695d/html/js/target/util_Q_3bb82a6eabd3339d91ca15cb4fd6685c.js

Follows the line 10 of my file: 遵循我文件的第10行:

console = console ? console : { log : function() {}, debug : function() {}}; 

The strange thing is that the same code works perfectly on browsers and IOS webview. 奇怪的是,相同的代码可以完美地在浏览器和IOS Webview上正常工作。

Is there somebody that could give me a hint about what is happening? 是否有人可以给我一些提示?

Sorry guys, unfortunally my code example was incomplete. 抱歉,很遗憾,我的代码示例不完整。 The problem was that javascript was setting the Strict Mode 问题是javascript设置了严格模式

(function($) {

    'use strict';

    /* 
     * 
     */
    jQuery.ajaxSettings.traditional = true;

    console = console ? console : { log : function() {}, debug : function() {}}; 

    window.c = console;

/*
* ...
*/

Specificacion says: 规范说:

With strict mode, you can not, for example, use undeclared variables. 在严格模式下,例如,您不能使用未声明的变量。

So, the correct way is: 因此,正确的方法是:

var console = window.console = window.console ? window.console : { log : function() {}, debug : function() {}};

The only question still open is why other browsers like Chrome, Firefox and ios it works 唯一仍未解决的问题是,为什么其他浏览器(例如Chrome,Firefox和ios)可以正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM