0

I would like to use jQuery in a WinForms WebBrowser control, but without getting access to jQuery via a link to a url (i.e. I want to embed jQuery in my app and get it from there). Is there a way to do that? If so, how does it need to be embedded (e.g. as a Content file) and what's the html to use it?

Cincy Steve
  • 457
  • 6
  • 16

1 Answers1

1

It seems pretty straight forward. Just grab the file, load it into a script element, and then add that to the DOM.

Here is how I would approach it:

Download it from here : https://code.jquery.com/jquery-2.2.4.min.js or here https://code.jquery.com/jquery/

load it into a file using File.ReadAllText Then insert it into the DOM.

Here is how you can do that :

    private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        WebBrowser wb = sender as WebBrowser;

        HtmlElement he = wb.Document.CreateElement("script");
        string jquery = System.IO.File.ReadAllText("jquery.js");
        he.InnerHtml = jquery;
        wb.Document.Body.InsertAdjacentElement(HtmlElementInsertionOrientation.AfterEnd, he);

    }

You could also inject it from a cdn like so :

 private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {  
        WebBrowser wb = sender as WebBrowser;

        HtmlElement he = wb.Document.CreateElement("script");
        mshtml.HTMLScriptElement script = he.DomElement as mshtml.HTMLScriptElement;
        script.src = "https://code.jquery.com/jquery-3.1.1.min.js";
        wb.Document.Body.InsertAdjacentElement(HtmlElementInsertionOrientation.AfterEnd, he);

    }
derekantrican
  • 1,891
  • 3
  • 27
  • 57
Alexander Ryan Baggett
  • 2,347
  • 4
  • 34
  • 61
  • You can also grab it via a cdn. – Alexander Ryan Baggett Feb 09 '17 at 21:55
  • Thanks. Tried your suggestions, but ran into these problems: (1) opened the 2.2.4.min file after download just to look at it and got an error on line 2 char 7332 saying 'n' is null or not an object (code: 800A138F). (2) creating the script element in the DocCompleted event handler using 1st approach caused the event handler to end before executing other code I have there. (3) bypassed #2 by directly adding the script in my initial html code (file contents bracketed by – Cincy Steve Feb 12 '17 at 15:47
  • As for 'addEventListener', that is a web browser control specific issue. You have to keep in mind that the web browser control uses the IE 7 rendering engine by default. You will have to modify the registry setting that corresponds the rendering mode you want for that page. Take a look at https://msdn.microsoft.com/en-us/library/ee330730(v=vs.85).aspx#browser_emulation – Alexander Ryan Baggett Feb 14 '17 at 16:27
  • 1
    I'm under the impression the following html which I have included gets past the default IE 7 rendering. I am running IE 11 on my computer. – Cincy Steve Feb 15 '17 at 21:03
  • Nope. If you use Fiddler to see the requests the web browser control uses, you will see it even uses the IE7 user-agent string. Unless that registry entry is set it is still going to try and render it using IE7 standards. In other words, unless that registry is set, the highest it can do is IE7. Which is why the nonstandard attachevent works, but addeventlistener doesn't – Alexander Ryan Baggett Feb 16 '17 at 20:55
  • This is documented both by [Microsoft](https://blogs.msdn.microsoft.com/patricka/2015/01/12/controlling-webbrowser-control-compatibility) and [users](https://weblog.west-wind.com/posts/2011/may/21/web-browser-control-specifying-the-ie-version). This has been discussed elsewhere on [stackoverflow](http://stackoverflow.com/questions/23776951/set-wpf-webbrowser-control-to-use-ie10-mode) too. In fact, you can try alert(navigator.userAgent); if you are still not sure. – Alexander Ryan Baggett Feb 16 '17 at 21:06
  • Thanks for the clear and thorough answer. Too bad this limitation exists. It would be helpful to be able to embed and use jQuery. – Cincy Steve Feb 18 '17 at 21:52
  • thanks for answer but tried the same but not working in mine. – nativegrip Jun 12 '18 at 04:54