locked
? .vb equivalent of .js document.getElementsByTagName("A")[0].removeAttribute("target"); ? RRS feed

  • Question

  • Hello & Thanks ;

    What is the  .vb equivalent of .js document.getElementsByTagName("A")[0].removeAttribute("target"); 

    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
    <h1>Hello World</h1>
    <a href="http://vmars.us/reads/ListenReadWatch.html" target="_blank">vmars.us/reads/ListenReadWatch.html</a>
    <button onclick="myFunction()">Please Stop me!</button>
    <script>
    function myFunction() {
      document.getElementsByTagName("A")[0].removeAttribute("target"); 
    }
    </script>
    </body>
    </html>
    

    Thanks for your Help...


    "All things in moderation, except for love and forgiveness."...vm

    Monday, August 17, 2020 1:45 AM

Answers

  • I ran this against your HTML in a WebBrowser control and worked. Note I didn't remove the target but changed it to _self which does the same as removing it.

    Private Sub ChangeLinkTargetButton_Click(sender As Object, e As EventArgs) _
        Handles ChangeLinkTargetButton.Click
    
        Dim linkElements As HtmlElementCollection =
                WebBrowser1.Document.GetElementsByTagName("A")
    
        If linkElements.Count > 0 Then
            Dim firstLink = linkElements.Cast(Of HtmlElement).FirstOrDefault()
            If firstLink.GetAttribute("target") = "_blank" Then
                firstLink.SetAttribute("target", "_self")
            End If
        End If
    
    End Sub


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by vmars316 Wednesday, August 19, 2020 12:40 AM
    Tuesday, August 18, 2020 2:47 PM

All replies

  • use WebBrowser.Document.InvokeScript to call the function.

    You can also rewrite the function with HtmlDocument.GetElementsByTagName(String) and HtmlElement.SetAttribute(String, String)



    Visual C++ MVP

    Monday, August 17, 2020 4:05 AM
  • Thanks Sheng Jiang  ;

    Hmm... that looks promising ;

    What would that look like in .vb ? 

    Or where can I view example in .vb

    Thanks


    "All things in moderation, except for love and forgiveness."...vm

    Monday, August 17, 2020 9:26 PM
  • Hi vmars316,
    You can convert the c# code in the document provided by Sheng Jiang into vb code by referring to the following picture.

    Best Regards,
    Daniel Zhang


    "Windows Forms General" forum will be migrating to a new home on Microsoft Q&A (Preview)!
    We invite you to post new questions in the "Windows Forms General" forum’s new home on Microsoft Q&A (Preview)!
    For more information, please refer to the sticky post.

    Tuesday, August 18, 2020 8:16 AM
  • Thanks Daniel ;

    L'll try to figure out what is the Syntax for .vb . 

    Are you saying I should Post this post "?" on Microsoft Q&A (Preview)!

    I Posted a ? on there yesterday , but I can see no evidence of it . 

    Thanks


    "All things in moderation, except for love and forgiveness."...vm

    Tuesday, August 18, 2020 1:20 PM
  • I ran this against your HTML in a WebBrowser control and worked. Note I didn't remove the target but changed it to _self which does the same as removing it.

    Private Sub ChangeLinkTargetButton_Click(sender As Object, e As EventArgs) _
        Handles ChangeLinkTargetButton.Click
    
        Dim linkElements As HtmlElementCollection =
                WebBrowser1.Document.GetElementsByTagName("A")
    
        If linkElements.Count > 0 Then
            Dim firstLink = linkElements.Cast(Of HtmlElement).FirstOrDefault()
            If firstLink.GetAttribute("target") = "_blank" Then
                firstLink.SetAttribute("target", "_self")
            End If
        End If
    
    End Sub


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by vmars316 Wednesday, August 19, 2020 12:40 AM
    Tuesday, August 18, 2020 2:47 PM
  • Thank you very much Karen ;

    I will test this out today :)

    First , I have to figure out "VS vb.Net .vb How to clone/copy a Repo LOCALLY (C:\) and give it a new name ?" so I don't loose what I have already 

    • Edited by vmars316 Tuesday, August 18, 2020 3:49 PM
    Tuesday, August 18, 2020 3:43 PM
  • Thank you very much Karen ;

    I will test this out today :)

    First , I have to figure out "VS vb.Net .vb How to clone/copy a Repo LOCALLY (C:\) and give it a new name ?" so I don't loose what I have already 

    You might consider using GitHub which is totally free and work is done directly in Visual Studio. You can push a Visual Studio solution to a repository, create branches. Let's say you need to revert to a former version that is a few clicks.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, August 18, 2020 4:01 PM
  • Thanks ;

    GitHub , that's a whole new language to me . 

    Isn't there another way , just using Visual Studio ? 

    Thanks


    "All things in moderation, except for love and forgiveness."...vm

    Tuesday, August 18, 2020 9:26 PM
  • Thanks ;

    GitHub , that's a whole new language to me . 

    Isn't there another way , just using Visual Studio ? 

    Thanks


    "All things in moderation, except for love and forgiveness."...vm

    Visual Studio does not have this ability, alternates

    • Close Visual Studio, with Windows Explorer, copy the folder then do CTRL+V and a copy of the folder will be created.
    • Windows 10, select the folder for your solution and right click and compress all
    • Install DPack (lots of cool stuff for free, been using it for over five years), use the Solution backup to compress all projects

    I recommended GitHub because for someone new to it in less than one hour you not only have a backup but incremental backups and branches.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, August 18, 2020 9:52 PM
  • Thanks Karen ;

    [Visual Studio does not have this ability, alternates

    Good idea , Done ! 

    Also downloaded DPack .

    Thanks


    "All things in moderation, except for love and forgiveness."...vm

    Wednesday, August 19, 2020 12:38 AM
  • Hi Karen ; 
    When I run this code 
    Handles ChangeLinkTargetButton.Click
    I get this error:
    1>-
    Build started: project: WindowsApp10, Configuration: Debug Any CPU
    1>C: error BC30506: Handles clause requires a WithEvents variable
    Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped

    The server keeps giving me an error and cancelling my REPLY .
    So if server doesnot fail , I will Post code on a second Post .

    Thanks for your Help...

    "All things in moderation, except for love and forgiveness."...vm

    Friday, August 21, 2020 2:58 PM
  • What this means is you (and it's easy to not realize this) copied and pasted that code into the form while the proper way is to create a button named ChangeLinkTargetButton, double click the button and place code into it.

    Under the covers Visual Studio creates the button like this.

    Friend WithEvents ChangeLinkTargetButton As Button


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, August 21, 2020 5:22 PM
  • Thanks Karen ;

    I tried putting  ChangeLinkTargetButton_Click()
    Into :

        Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            HitYes = "no"
            txbAddress.Text = WebBrowser1.Url.ToString
            Textboxt2LinesCount = TextBox2.Lines.Length()
            For i = 0 To Textboxt2LinesCount - 1
                '            MsgBox("Textboxt2LinesCount =  " & i)
                If txbAddress.Text = TextBox2.Lines(i) Then
                    MsgBox("We have a hit people = " & TextBox2.Lines(i))
                    CheckingLine = TextBox2.Lines(i)
                    HitYes = "yes"
    
                    Sub Target_self_Click()
    
                    Exit For
            End If
            Next
            If HitYes = "no" Then
                MsgBox("We have a NO HITs people = " & CheckingLine)
            End If
        End Sub
    But then got errors in :
        Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

     Where should it be placed ?

    Thanks 



    "All things in moderation, except for love and forgiveness."...vm

    Friday, August 21, 2020 8:52 PM
  • So what is the error, I can't guess what the error is.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, August 21, 2020 9:44 PM
  • Sorry :)

    my keyboard just died

    thank God for onScreen keyboard . 

    Here is the error msg: 

    Thanks


    "All things in moderation, except for love and forgiveness."...vm

    Saturday, August 22, 2020 10:34 PM
  • Please hold on , I will compile it again with 

    Call Target_self_Click()

    instead of :

    Sub Target_self_Click()

    Thanks


    "All things in moderation, except for love and forgiveness."...vm

    Sunday, August 23, 2020 6:15 PM
  • Hello ;

    Compiling with the code below yields this error :

    1>C:\Users\vmars\source\repos\WindowsApp10\Form1.vb(21,13): error BC30506: Handles clause requires a
    WithEvents variable defined in the containing type or one of its base types. 

    Statement 21 =

    1>C:\Users\vmars\source\repos\WindowsApp10\Form1.vb(21,13): error BC30506: Handles clause requires a
    WithEvents variable defined in the containing type or one of its base types.

        Private Sub ChangeLinkTargetButton_Click(sender As Object, e As EventArgs) _
        Handles ChangeLinkTargetButton.Click
    
            Dim linkElements As HtmlElementCollection =
                WebBrowser1.Document.GetElementsByTagName("A")
    
            If linkElements.Count > 0 Then
                Dim firstLink = linkElements.Cast(Of HtmlElement).FirstOrDefault()
                If firstLink.GetAttribute("target") = "_blank" Then
                    firstLink.SetAttribute("target", "_self")
                End If
            End If
    
        End Sub
    
        Private Sub Target_self_Click(sender As Object, e As EventArgs) Handles Target_self.Click
            ChangeLinkTargetButton_Click()
        End Sub
    
        Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            HitYes = "no"
            txbAddress.Text = WebBrowser1.Url.ToString
            Textboxt2LinesCount = TextBox2.Lines.Length()
            For i = 0 To Textboxt2LinesCount - 1
                '            MsgBox("Textboxt2LinesCount =  " & i)
                If txbAddress.Text = TextBox2.Lines(i) Then
                    MsgBox("We have a hit people = " & TextBox2.Lines(i))
                    CheckingLine = TextBox2.Lines(i)
                    HitYes = "yes"
                    Call Target_self_Click()
                    Exit For
            End If
            Next
            If HitYes = "no" Then
                MsgBox("We have a NO HITs people = " & CheckingLine)
            End If
        End Sub

    Thanks


    "All things in moderation, except for love and forgiveness."...vm

    Sunday, August 23, 2020 9:22 PM
  • Oops!

    Statement 21 =

        Handles ChangeLinkTargetButton.Click


    "All things in moderation, except for love and forgiveness."...vm

    Sunday, August 23, 2020 9:24 PM