How to embed js file in wpf control of web browser in C #

Hey. I am using a WPF WebBrowser control on my xaml page. I have html, jquery, js and css file in my html package. I need to load an html page in a web browser with the whole jquery library. How can I insert this file? I write the code as follows:

page.xaml.cs

void wb_Loaded(object sender, RoutedEventArgs e) { string htmlPath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "keyboardWidget\\test.html"); string html = File.ReadAllText(htmlPath.ToString()); webBrowser.NavigateToString(html); } 

this is page.xaml:

 <WebBrowser x:Name="webBrowser"></WebBrowser> 

I get an error like this.

enter image description here

Hi, can anyone suggest the best way to add a js or jquery library. if this is not possible in managing a wpf web browser, then what kind of control should I use.

+5
source share
3 answers

I suspect that the problem may not be that the JS script control was not found by the WebBrowser control, but that the WPF WebBrowser control has JSON compatibility issues.

Try including this script on your page and see if it fixes the problem: JSON2

Download json2.js and include this in the HTML of your page:

 <script type="text/javascript" src="json2.js"></script> 
+1
source

I was not able to display html with js file in WPF WebBrowser control.

Then I used the GeckoFx control.

Add xulrunner, Geckofx-Winforms.dll and Geckofx-Core.dll then I added page.xaml

  <Grid x:Name="gecoBrowser" Height="150" Width="735"> 

page.xaml.cs:

  Gecko.Xpcom.Initialize(AppDomain.CurrentDomain.BaseDirectory + "xulrunner"); Gecko.GeckoWebBrowser myBrowser = new Gecko.GeckoWebBrowser(); WindowsFormsHost host = new WindowsFormsHost(); host.Child = myBrowser; gecoBrowser.Children.Add(host); string htmlPath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "keyboardWidget\\test.html"); myBrowser.Navigate("file:///" + htmlPath); 
0
source

Hi found the answer to this question: insert the js file into the wpf web browser.

first you need to update your IE in html, as in the html header:

  <meta http-equiv="x-ua-compatible" content="IE=9"> 

enter this jquery library: -

 <script src="http://code.jquery.com/jquery-1.10.2.js"></script> 

Last thing:

https://forum.jquery.com/topic/json-is-undefined-with-jquery-min-2-0-3-on-ie-11

IE 11 mode registry:

https://weblog.west-wind.com/posts/2011/May/21/Web-Browser-Control-Specifying-the-IE-Version

0
source

All Articles