I am using the following code to override the console.log
function, because I want to print console.log
only if showConsole
returns true
.
var proxyConsolelog = window.console.log;
console.log=function(msg){
try{
if(Boolean(showConsole))
{
proxyConsolelog(msg);
}
}catch(e){
alert(JSON.stringify(e.message));
proxyConsolelog('ERROR-->>'+e.message);
}
}
The proxyConsolelog
line creates a problem, and alert(JSON.stringify(e.message));
is giving me a "Type error".
And I get this:
void SendDelegateMessage(NSInvocation *): delegate (webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:) failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode
in the log.
How can I achieve this?
I am using the following code to override the console.log
function, because I want to print console.log
only if showConsole
returns true
.
var proxyConsolelog = window.console.log;
console.log=function(msg){
try{
if(Boolean(showConsole))
{
proxyConsolelog(msg);
}
}catch(e){
alert(JSON.stringify(e.message));
proxyConsolelog('ERROR-->>'+e.message);
}
}
The proxyConsolelog
line creates a problem, and alert(JSON.stringify(e.message));
is giving me a "Type error".
And I get this:
void SendDelegateMessage(NSInvocation *): delegate (webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:) failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode
in the log.
How can I achieve this?
Share Improve this question edited Aug 12, 2013 at 15:09 Ry-♦ 226k56 gold badges493 silver badges499 bronze badges asked Aug 12, 2013 at 15:03 Prince Kumar SharmaPrince Kumar Sharma 12.6k4 gold badges62 silver badges90 bronze badges 2-
Have you tried
proxyConsolelog.apply(console, arguments);
? – plalx Commented Aug 12, 2013 at 15:09 -
I don't know what's
showConsole
but I doubt it's useful to doBoolean(showConsole)
. – Denys Séguret Commented Aug 12, 2013 at 15:46
1 Answer
Reset to default 10The problem you have is that the receiver (this
) when you call your function, isn't the console.
You can do this :
var proxyConsolelog = window.console.log.bind(window.console);
If you need to be patible with IE8 (which doesn't have bind
), you may do this :
var logFun = window.console.log;
var proxyConsolelog = function(){
logFun.apply(window.console, arguments)
};
As you tagged the question jquery, then you may also use proxy :
var proxyConsolelog = $.proxy(window.console.log, window.console);
Once you have your new function, you can call it just like console.log
:
proxyConsolelog('some', {arg:'uments'});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745194072a4616005.html
评论列表(0条)