Unable to use Console.Log() within a WebBrowser Control

前端 未结 2 1154
情歌与酒
情歌与酒 2020-12-17 04:43

So I\'m writing a Javascript coding UI using C# Windows Forms. This is my code for when the \"Run\" button is pressed, in case it helps:



        
相关标签:
2条回答
  • 2020-12-17 05:20

    Yes, Console class is not available in your browser control, but you can create a logger class like this

    [ComVisible(true)]
    public class Logger
    {
        public void log(string s)
        {
            Console.WriteLine(s);
        }
    }
    

    and use it in your browser control

    webBrowser1.ObjectForScripting = new Logger();
    webBrowser1.DocumentText = "<script>external.log('TEST');</script>";
    
    0 讨论(0)
  • 2020-12-17 05:36

    You can get JavaScript console output from within Visual Studio.

    By default the webBrowser1 control uses IE7 to render it's output. IE7 does not have a console.log() function. In order to get the console.log() function to work, you need to add the following meta tag:

    <meta http-equiv="X-UA-Compatible" content="IE=11">
    

    'IE=8' or greater should make the console.log() available to you.

    When you debug a Windows Forms application it debugs using the .NET Managed Code debugger. In order to debug differently, instead of pressing 'Play' to debug, try selecting "Debug" > "Start without Debugging". Now once your application is running, go to "Debug" > "Attach to Process" and find your WindowsFormsApplication.exe, attach to it using the Script Code Debugger instead of the .NET Managed Code debugger.

    Now, in Visual Studio:

    • You can open "Debug" > "Windows" > "JavaScript Console"
    • You can also open "Debug" > "Windows" > "DOM Explorer"
    0 讨论(0)
提交回复
热议问题