none
Programmatically How to Enable and Disable JavaScript of a WebBrowser Control in Windows Form application using C#

    Question

  • Hello Everyone,

    Is it possible to Programmatically Enable and Disable JavaScript of WebBrowser Control in Windows Form application using C#? If yes can someone share the code snippet. Thanks.

    Regards,
    Ramesh

    Monday, September 14, 2009 5:08 PM

All replies

  • Hi Ramesh,

    Do you mean the "System.Windows.Forms.WebBrowser" control?

    WebBrowser control doesn't have such property to set to enable or disable javascript. If you want to enable/disable javascript, you can enable/disable it in your Internet Security option in the Windows system. That option can be found as below:
    Control Panel -> Internet Options -> Security

    Here is a related topic
    http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/3264223f-d5c0-4c1a-b550-3621ca17635f/

    Just like Zhi-Xin Ye's reply, enable/disable javascript relate to security settings of IE. Change security settings via code is not recommended. Do you have any reason to enable/disable javascript via code instead of setting the IE security settings?

    Sincerely,
    Kira Qian
    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework!
    Tuesday, September 15, 2009 9:01 AM
  • Yes, its "System.Windows.Forms.WebBrowser" control.

    I also know WebBrowser control doesn't have such property, but sometime time back I have seen someone had provided a solution where you can add some configuration settings in your app.config file which would allow you to disable JavaScript only for that session, it doesn't modify any of the IE settings. Unfortunatly I am unable to locate that information.

    As far as any specific reason to enabling and disabling this programmatically? then the answer is yes, the reason is, we have a application which displays news information using marquee control (scrolling news) and we have noticed that, if this application runs on vmware then the CPU usage is high (poor performance). On laptop or workstation its 0% where as on vmware it is ~20 - 30%. When I was searching for a solution, peopel where suggesting that you should disable JavaScript and which makes the difference. I have noticed the same by manually disabling JavaScript. Now I don't want to make these IE settings related change on user's desktop insted try to find a way to enable disable programmatically which would be applicable to that perticular session.

    Regards,
    Ramesh 
    Tuesday, September 15, 2009 2:11 PM
  • Hi Ramesh,

     

    I have known your requirement more clearly this time. I have searched a lot for disable javascript for the current webbrowser control without change IE security settings, but it is unlucky that I cannot find a perfect solution.

     

    However, I found the following topic at last

    http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Controls/Q_20569165.html

    The solution given by Rhaedes had been accepted by the thread owner. According to his suggestion, I have written the code below to implement it.

    using System.Web;

    using System.Net;

    using System.IO;

    using System.Text.RegularExpressions;

     

    public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

     

            private void button1_Click(object sender, EventArgs e)

            {

                string url = "http://www.msn.com";

                string htmlDocText = TrimScript(GetHtmlContent(url));

                File.WriteAllText("D:\\temp.htm", htmlDocText);

                webBrowser1.Url = new Uri("D:\\temp.htm");

            }

     

            private string GetHtmlContent(string url)

            {

                string htmlContentText = String.Empty;

                HttpWebRequest httpWebRequest = null;

                HttpWebResponse httpWebResponse = null;

                StreamReader streamReader = null;

                try

                {

                    httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

                    httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

                    httpWebRequest.Timeout = 5000;

     

                    streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.Default);

                    htmlContentText = streamReader.ReadToEnd();

                    streamReader.Close();

                    httpWebResponse.Close();

                }

                catch { }

                finally

                {

                    httpWebResponse.Close();

                    streamReader.Close();

                }

                return htmlContentText;

            }

     

            private string TrimScript(string htmlDocText)

            {

                string bodyText = "";

                string trimJavascript = "<script type=\"text/javascript\">(.*?)</script>";

                Regex regexTrimJs = new Regex(trimJavascript);

                bodyText = regexTrimJs.Replace(htmlDocText, "");

                return bodyText;

            }

    }

    The GetHtmlContent method will download the page of a special URL. The TrimScript method will use regular expression to trim the javascript. So I can save the html document without javascript to the disk. At last, I will load the page from that temp html file.

     

    I have tested the msn web site, after I did the above step in my code, I cannot find javascript in the page source.

     

    Remark:

    If the javascript block is not defined as <script type=\"text/javascript\">...</script>, you can change the regular expression a little to match that format. Otherwise, it won’t be cleared  thoroughly.

     

    Hope you can benefit from this solution.

     

    Sincerely,

    Kira Qian

    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    Wednesday, September 16, 2009 8:33 AM
  • Thank you Kira for the updates. As far as trimming JavaScript, that I am already doing it. Again the issue is only on vmware. When I trurn off JavaScript manually, the CPU usage is very low (~0 - ~3%).

    Any other suggestions?

    Regards,
    Ramesh
    Thursday, September 17, 2009 8:11 PM
  • Hi Ramesh,

     

    Do you mean trimming javascript does help on real machine but does not on VMWare? As far as I know, you cannot disable javascript for a session. It is configured globally. If you want to understand it better, you can try to post on Internet Explorer Development forums.

     

    If you have any other Winform question, please feel free to post on Winform forums.

     

    Sincerely,

    Kira Qian

    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    Friday, September 18, 2009 9:00 AM
  • Better solution of trimming away javascript, instead of using Regex as in previous comment:

    private string TrimScript(string htmlDocText)

    {

    while (htmlText.ToLower().IndexOf("<script type=\"text/javascript\">") > -1) { int s_index = htmlText.ToLower().IndexOf("<script type=\"text/javascript\">"); int e_index = htmlText.ToLower().IndexOf("</script>"); htmlText = htmlText.Remove(s_index, e_index - s_index); } return htmlText;

    }



    • Edited by Whiterocker Friday, October 05, 2012 7:27 PM
    Friday, October 05, 2012 7:27 PM
  • Just to make clear, regular expressions to strip Javascript are not robust.  Don't forget all the "on..." event attributes, various ways the <script> tag may be constructed, external scripts, script in CSS and more.  Please don't do this!
    Tuesday, February 05, 2013 1:00 AM