Results 1 to 5 of 5

Thread: embed javascript in page

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    embed javascript in page

    Embed a javascript code to the web browser page -

    I have a web page that I created, and testing with vb6 to use the IE object. I want to insert this piece of code to the page, and let it alert, but it doesn't work. Is this possible?

    Dim s as string
    s= <script>alert('test');</script>
    document.body.insertAdjacentHTML "afterbegin", s

    Expected result is that I would like to get an alert.

  2. #2
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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 ?


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    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?

  4. #4
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: embed javascript in page

    I got this to work

    Code:
     Dim s As String
    
     s = "&nbsp;<SCRIPT DEFER>{alert('Hello from inserted script.');}</script>"
    
     WebBrowser2.Document.body.insertAdjacentHTML "beforeBegin", s
    NOTE: I had to add the &nbsp; to make it a valid HTML line plus according to MS you need to include the DEFER argument.

    Without the &nbsp; 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 &nbsp; is (I think any valid HTLM element will make it work)
    Last edited by jmsrickland; Aug 7th, 2012 at 06:08 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  5. #5
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width