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!