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
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With