3

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

1
  • Any updates? Could u did it? Commented Feb 7, 2013 at 13:18

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

The idea is to load LOCAL JavaScript file. (Application should work without Internet)
1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.