Android Question Insert javascrip date

GERSON PINTO

Member
Licensed User
Hello
I'm trying to insert the current date into a page loaded (at end) into a webviewextras, but the code below is not working.
Can someone help me? I'm not very familiar with javascript...
B4X:
Sub WebView1_PageFinished (Url As String)

Dim javascript2 As StringBuilder
    javascript2.Initialize
    javascript2.Append("var toInsert = document.createElement('div');")
    javascript2=javascript2.Append("var today = new Date().toLocaleDateString();")
    javascript2=javascript2.Append("toInsert.innerHTML =today;")
    javascript2=javascript2.Append("document.body.appendChild(today);")
    
WebView1.ExecuteJavascript(javascript2)
End Sub
 

GERSON PINTO

Member
Licensed User
Application requirement Erel
The webpage must be downloaded and should display text and date inside it
preferably at the top of the page not on bottom, but I don't known how to do it...
 
Upvote 0

Brandsum

Well-Known Member
Licensed User
Hello
I'm trying to insert the current date into a page loaded (at end) into a webviewextras, but the code below is not working.
Can someone help me? I'm not very familiar with javascript...
B4X:
Sub WebView1_PageFinished (Url As String)

Dim javascript2 As StringBuilder
    javascript2.Initialize
    javascript2.Append("var toInsert = document.createElement('div');")
    javascript2=javascript2.Append("var today = new Date().toLocaleDateString();")
    javascript2=javascript2.Append("toInsert.innerHTML =today;")
    javascript2=javascript2.Append("document.body.appendChild(today);")
   
WebView1.ExecuteJavascript(javascript2)
End Sub
B4X:
Sub WebView1_PageFinished (Url As String)

Dim javascript2 As StringBuilder
    javascript2.Initialize
    javascript2.Append("var toInsert = document.createElement('div');")
    javascript2=javascript2.Append("var today = new Date().toLocaleDateString();")
    javascript2=javascript2.Append("toInsert.innerHTML =today;")
    javascript2=javascript2.Append("document.body.appendChild(today);")  '<- this should be toInsert instead of today
    
WebView1.ExecuteJavascript(javascript2)
End Sub
 
Upvote 0

GERSON PINTO

Member
Licensed User
B4X:
document.body.prepend(toInsert); ' add element at the top

document.body.appendChild(toInsert); 'add element at the bottom

Perfect, but the text is being inserted over the text of the page. Would it be possible to add add blank lines before inserting?
 
Upvote 0

Brandsum

Well-Known Member
Licensed User
You need CSS and Javascript knowledge for adding or modifying HTML DOM elements style.

B4X:
Sub WebView1_PageFinished (Url As String)

Dim javascript2 As StringBuilder
    javascript2.Initialize
    javascript2.Append("var toInsert = document.createElement('div');")
    javascript2=javascript2.Append("var today = new Date().toLocaleDateString();")
    javascript2=javascript2.Append("toInsert.innerHTML =today;")

    javascript2=javascript2.Append("toInsert.style.marginBottom = '5px'")
    javascript2=javascript2.Append("toInsert.style.padding = '5px'")
    javascript2=javascript2.Append("toInsert.style.borderBottom = 'thin solid #888'")
    javascript2=javascript2.Append("toInsert.style.textAlign = 'center'")

    javascript2=javascript2.Append("document.body.prepend(toInsert);")
  
WebView1.ExecuteJavascript(javascript2)
End Sub
 
Upvote 0

GERSON PINTO

Member
Licensed User
You need CSS and Javascript knowledge for adding or modifying HTML DOM elements style.

B4X:
Sub WebView1_PageFinished (Url As String)

Dim javascript2 As StringBuilder
    javascript2.Initialize
    javascript2.Append("var toInsert = document.createElement('div');")
    javascript2=javascript2.Append("var today = new Date().toLocaleDateString();")
    javascript2=javascript2.Append("toInsert.innerHTML =today;")

    javascript2=javascript2.Append("toInsert.style.marginBottom = '5px'")
    javascript2=javascript2.Append("toInsert.style.padding = '5px'")
    javascript2=javascript2.Append("toInsert.style.borderBottom = 'thin solid #888'")
    javascript2=javascript2.Append("toInsert.style.textAlign = 'center'")

    javascript2=javascript2.Append("document.body.prepend(toInsert);")
 
WebView1.ExecuteJavascript(javascript2)
End Sub

Great and poweful!
Thank you so much brandsum
 
Upvote 0
Top