B4J Question Webview get result from click on a link

Discussion in 'B4J Questions' started by TomDuncan, Jul 1, 2015.

  1. TomDuncan

    TomDuncan Active Member Licensed User

    Hi All,
    I have a app that generates a web page locally.
    I have Links inside this which I need too trigger events.
    Can I find when a link has been pressed.
    I have tested LocationChanged but no luck.
    I just need to put in my own value in the href= area.
    Then get this value
    i.e.
    Code:
    <a href="$COMMENT$">comment</a><br>
    Tom
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    LocationChanged should be raised. Though it is not so easy to work with this event as you cannot cancel it. You need to reload the original url each time.
     
  3. TomDuncan

    TomDuncan Active Member Licensed User

    On my testing it was not raised.
    But maybe because it is not a valid place to go to.
    Will check if I can put in a marker on the page to jump to.
    Tom
     
  4. TomDuncan

    TomDuncan Active Member Licensed User

    Just did some more tests.
    I added an anchor to send to the top of the file.
    Any nothing works.
    This web view uses LoadHtml( MY BUILT HTML FILE)

    Is this an error with Loadhtml?

    Tom
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Check this example:
    Code:
    Sub Process_Globals
       
    Private fx As JFX
       
    Private MainForm As Form
       
    Private wv As WebView
    End Sub

    Sub AppStart (Form1 As Form, Args() As String)
       MainForm = Form1
       MainForm.Show
       wv.Initialize(
    "wv")
       MainForm.RootPane.AddNode(wv, 
    1010300300)
       wv.LoadHtml(
    $"<a href="http://www.example.com">link</a>"$)
    End Sub

    Sub wv_LocationChanged (Location As String)
       
    Log(Location)
       wv.LoadHtml(
    $"<a href="http://www.example.com">link</a>"$)
    End Sub
     
  6. TomDuncan

    TomDuncan Active Member Licensed User

    Yep, that works.

    The only difference with mine in that I have created the string then pass
    That string to the loadhtml
    What is the $ top and tail of the string.
    I am still a newbie.

    Tom
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    It doesn't matter whether you put the string directly or in a variable.

    [B4X] Smart String Literal
     
  8. TomDuncan

    TomDuncan Active Member Licensed User

    With your demo, can I catch the event before it executes the go to the location?
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    Please try it.
     
  10. TomDuncan

    TomDuncan Active Member Licensed User

    Oops. Yes i did and it works.
    Only as a link to a http not as an anchor.
    So when I reload the page it reloads from the top.

    Will I have to get the position before the action happens then move the cursor
    To the old location.

    Tom
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    It is not easy to work with this event as you need to reload the page. There is no simple way to get the current position. You will need to inject JavaScript for that.
     
  12. TomDuncan

    TomDuncan Active Member Licensed User

    Thanks.
    Will work out another way to do it.
    Tom
     
Loading...