Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include Jquery for web browser control in wpf

I am using web browser control in my project ,i can display html data easily with this control,
now i am trying to include jquery in this html but any how it does not seems to be working

  WebBrowser webwsr = new WebBrowser();
  String WebBrwseHTML = "<html><head><script type='text/javascript' src='jquery-1.7.1.js'></script><script type='text/javascript'>$(document).ready(function () {  $('div').css('background-color', 'Red'); })</script></head><body><div>DUMMY</div></body></html>";

   webwsr.NavigateToString(WebBrwseHTML);

what i am doing wrong here

like image 417
Buzz Avatar asked Aug 29 '26 09:08

Buzz


2 Answers

not a big jq expert but try this:

 StringBuilder sb = new StringBuilder();
        sb.AppendLine("<html>");
        sb.AppendLine("<head>");
        sb.AppendLine(" <script src='http://code.jquery.com/jquery-latest.js'></script>");
        sb.AppendLine("<script>");
        sb.AppendLine("$(document).ready(function () {");
        sb.AppendLine("$('div').css('background-color', 'Red'); });");
        sb.AppendLine("</script>");
        sb.AppendLine("</head>");
        sb.AppendLine("<body>");
        sb.AppendLine("<div>DUMMY</div>");
        sb.AppendLine("</body>");
        sb.AppendLine("</html>");

        WebBrowser webwsr = new WebBrowser();
        String WebBrwseHTML = sb.ToString();
        webwsr.NavigateToString(WebBrwseHTML);
        mainGrid.Children.Add(webwsr);
like image 84
Yoav Avatar answered Aug 31 '26 02:08

Yoav


My recommendation would be to use System.IO.File.ReadAllText(jqueryFilePath) to read the base jQuery code, and then instead of <script src="..."></script> use <script>" + jquery + "</script>.

Below is a working example: (replace the @"C:\jquery.txt" with your own path)

var jquery = File.ReadAllText(@"C:\jquery.txt");

var html = @"<html>
<head>
    <script type='text/javascript'>"+jquery+@"</script>
    <script type='text/javascript'>
        $(document).ready(function() {
            $('div').css('background-color', 'Red');
        })
    </script>
</head>

<body>
    <div>DUMMY</div>
</body>

</html>";

browser.NavigateToString(html);
like image 45
Jakub Loksa Avatar answered Aug 31 '26 01:08

Jakub Loksa