Re: embed javascript in page
I can get it to work if I use this:
s = "<script>alert('test');</script>"
WebBrowser1.Document.Write s
but not using
WebBrowser1.document.body.insertAdjacentHTML "afterbegin", s
What does "afterbegin" mean ?
Re: embed javascript in page
The afterend,beforeend, etc. are provided by Microsoft to determine "where" I want to input that line of code. That piece is working.
I just tried your method document.write, but I am getting a compile error:
Function or interface marked as restricted, or the function
uses an Automation type not supported in Visual Basic
How do I bypass that?
Re: embed javascript in page
I got this to work
Code:
Dim s As String
s = " <SCRIPT DEFER>{alert('Hello from inserted script.');}</script>"
WebBrowser2.Document.body.insertAdjacentHTML "beforeBegin", s
NOTE: I had to add the to make it a valid HTML line plus according to MS you need to include the DEFER argument.
Without the or any valid HTML element I couldn't get it to work; the <script> tag is not considered a HTML element, I guess, but the is (I think any valid HTLM element will make it work)
Re: embed javascript in page
Here is another example where it inserts JavaScript into an Image element which means you will need to click on the image to run the JavaScript.
Add Controls to Form1:
WebBrowser Control
Command1
Command2
Add References to Project
Click on Projects-->References then add
--> OLE Automation
--> Microsoft Internet Controls
Need:
An HTML page that contains an image tag. Note: if page has more than one image the script is added to each image tag unless you specify which image tag to use
Code:
Dim DoNotNavigate As Boolean
Dim MyURL As String
Private Sub Form_Load()
DoNotNavigate = False
End Sub
Private Sub Command1_Click()
MyURL = App.Path & "\your_html_page.html"
WebBrowser1.Navigate MyURL
End Sub
Private Sub Command2_Click()
Dim scriptNode As Object, headNode As Object
Dim oImages As Object, oImage As Object
Dim imageNode As Object
Set scriptNode = WebBrowser1.Document.createElement("SCRIPT") 'create a new <script> node
scriptNode.Type = "text/javascript" 'set type to javascript
scriptNode.Text = "function test(){alert('Hello from inserted JS in an Image Tag');}"
Set headNode = WebBrowser1.Document.getElementsByTagName("HEAD") 'select <head> node
Call headNode(0).appendChild(scriptNode) 'attach <script> node to <head>
Do While Not WebBrowser1.ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
Set oImages = WebBrowser1.Document.getElementsByTagName("img") 'select all <img> tags
For Each oImage In oImages
Set imageNode = WebBrowser1.Document.createElement("A") 'create a new <a> tag
imageNode.href = "javascript:test()"
imageNode.innerhtml = oImage.outerHTML 'place the <img> tag with all settings
imageNode.attachEvent "onclick", WebBrowser1.Document.parentWindow.test() 'attach event to the <a> tag
Call oImage.replaceNode(imageNode) 'replace the <img> tag, that is will be <a><img></a>
Next
End Sub
Private Sub WebBrowser1_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)
If URL = MyURL Then
Cancel = False
Else
Cancel = True
End If
End Sub