Android Question executeJavascript problem in webview

cbanks

Active Member
Licensed User
Why does the first section of code work, but not the second (Code is in the page finished sub)? It works (moves down to named anchor) when I don't use a variable, it doesn't work when I use a variable.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    FlingableWebView1.LoadUrl("http://alumnihighschool.net/scriptures/bm/1_ne/1.html")
End Sub

Sub FlingableWebView1_PageFinished (url As String)
    Dim Javascript As String
    Javascript = "window.location.hash='#1: 8'"
    WebViewExtras1.executeJavascript(FlingableWebView1, Javascript)
End Sub

B4X:
Sub Activity_Create(FirstTime As Boolean)
    FlingableWebView1.LoadUrl("http://alumnihighschool.net/scriptures/bm/1_ne/1.html")
End Sub

Sub FlingableWebView1_PageFinished (url As String)
    TagsAnchorName = "#1: 8"
    Dim Javascript As String
    Javascript = "window.location.hash='" & TagsAnchorName & "'"
    WebViewExtras1.executeJavascript(FlingableWebView1, Javascript)
End Sub
 
Last edited:

LucaMs

Expert
Licensed User
Why does the first section of code work, but not the second? It works (moves down to named anchor) when I don't use a variable, it doesn't work when I use a variable.

B4X:
    Dim Javascript As String
Javascript = "window.location.hash='#1: 8'"
WebViewExtras1.executeJavascript(FlingableWebView1, Javascript)

B4X:
TagsAnchorName = "#1: 8"
Dim Javascript As String
Javascript = "window.location.hash='" & TagsAnchorName & "'"
WebViewExtras1.executeJavascript(FlingableWebView1, Javascript)


I'm the only one that don't knows Javascript !
I try to guess: some characters need to be preceded by "\" (in your TagsAnchorName)
 
Upvote 0

LucaMs

Expert
Licensed User
I don't remember (my brain has few ram bytes :))
but some chars, in some "environment", need that char, backslash.

You can try:

TagsAnchorName = "\#1: 8"

or

TagsAnchorName = "\#1\: 8"

It is known as "escape char", used for "special" chars
 
Upvote 0

DonManfred

Expert
Licensed User
i would try it with
Javascript = "window.location.hash='#1: 8\'"

The ' is an special char which has to be escaped.

But i dont know wheter
#1: 8'
is an allowed anchor-name.

I would suggest to rename the anchorname to just chars and numbers (NOT beginning with a number)

->> #anchorname1

With such a name you would have less problems. btw: spaces are not allowed in anchors
 
Upvote 0
Top