How do you send console messages and errors to alert?

前端 未结 3 581
离开以前
离开以前 2020-12-09 18:28

I would like to pass errors to an alert to warn the user they made mistake in their code even if they don\'t have console open.

    var doc=(frame.contentWin         


        
3条回答
  •  攒了一身酷
    2020-12-09 18:54

    You could wrap the script in its own try/catch, something like:

    var doc=(frame.contentWindow.document || obj.contentDocument|| obj.contentWindow);
    var head = doc.getElementsByTagName('head')[0];
    var scriptElement = doc.createElement('script');
    scriptElement.setAttribute('type', 'text/javascript');
    scriptElement.text = "try{"+scripts+"}catch(e){console.error(e);alert('Found this error: ' + e +'. Check the console.')}"
    head.appendChild(scriptElement);
    

提交回复
热议问题