user1797147
user1797147

Reputation: 927

vba webbrowser and external javascript loading

I manually have a constructed web page in a string for displaying in WebBrowser control (part of code)

    ie.Document.clear

        str = "<!DOCTYPE html>" & vbCrLf _
            & "<html>" & vbCrLf _
            & "<head>" 

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

            & "<script>" & vbCrLf _
     & "$(function() {" _
    & "$( ""#dialog"" ).dialog();" _
    & "});" _
    & "</script>" & vbCrLf _
            & "</head><body>" & vbCrLf _

ie.Document.write str
ie.Document.Close

After many hours of investigation, I finally concluded that JQuery external *.js script isn't loaded at time that WB parser reaches the dialog() function. Even I put the file locally (with local path) isn't loaded.

The IE error is "The value of property $ is null or undefined...".

The HTML (constructed) code is OK because I did a very simple test. I saved the WB page source via right click -> page source, I saved locally as *.htm and back in MSACCESS, I use navigate WB's method

ie.Navigate "file:///V:/...file.htm"

And this is working just fine.

So the question is why

ie.Document.write str  -> not working
ie.Navigate -> works

I need the first method since I use WinHTTP to make a request, take some real page, parse and finally display in WB.

Of course, I could manipulate emulation from HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION which btw works but this will fall into several problems on other machines where I cannot have access to registry.

Thanks in advance,

Upvotes: 1

Views: 1632

Answers (2)

user6432984
user6432984

Reputation:

After testing the OP's answer I concluded that refreshing the WebBrowser does indeed load JQuery.

A few notes:

  • "" is required
  • WebBrowser.Document.Clear isn't needed
  • WebBrowser.Navigate or WebBrowser.Navigate2 can be used
  • WebBrowser.Refresh or WebBrowser.Refresh2 can be used

Test

Dim HTML As String
With CreateObject("System.Text.StringBuilder")
    .Append_3 "<!DOCTYPE html>" & vbCrLf
    .Append_3 "<HTML>" & vbCrLf
    .Append_3 "<HEAD>" & vbCrLf
    .Append_3 "<SCRIPT src=""http://code.jquery.com/jquery-1.10.2.js""></SCRIPT>" & vbCrLf
    .Append_3 "<SCRIPT>" & vbCrLf
    .Append_3 "$(function() {" & vbCrLf
    .Append_3 "$( ""#test"" ).css( ""border"", ""3px solid red"" );"
    .Append_3 "});"
    .Append_3 "</SCRIPT>" & vbCrLf
    .Append_3 "</HEAD>" & vbCrLf
    .Append_3 "<BODY>" & vbCrLf
    .Append_3 "<DIV id=""test"">Hey</DIV>"
    .Append_3 "</BODY>" & vbCrLf
    .Append_3 "</HTML>" & vbCrLf
    HTML = .ToString
End With

With WebBrowser1
    .navigate "About:Blank"
    .Silent = True
    .Document.Write HTML
    .Refresh
End With

Upvotes: 0

user1797147
user1797147

Reputation: 927

Got It! I noticed accidentally that right click refresh will load the JS :) Since I spent many hours to dig, here is the trick in case someone will need:

ie.Silent = True           ' <-- supress any JS warnins
ie.Document.clear
ie.Document.write str
ie.Refresh    ' <-- this will load the JQuery just fine

I really don't understant WTF Microsoft still use default ancient IE in emulated stuff including it's own products like Office?!

Upvotes: 2

Related Questions