B4J Question how to display html file in local directory???

phongka

Member
I have used WebView and StringBuilder to render the HTML page, but the result is not as expected. It seems that CSS or JavaScript is not working. If anyone can identify the error or provide a solution, please guide me. Thank you.

image.jpg


example:
Sub btnInitialize_Click
    'Log(xui.DefaultFolder)
    
    sb.Initialize
    sb.Append("StringBuilder").Append(CRLF).Append("After").Append("Initialize")
    sb.Initialize
    sb.Append("<sb>")
    sb.Append("<head>")
    sb.Append("<link href=""prism.css"" rel=""stylesheet"">")
    sb.Append("</head>")
    sb.Append("<body>")
    sb.Append("<pre><code class=""language-javascript"">")
    sb.Append("/* Your code here */")
    sb.Append("var b = 'Application_Error';")
    sb.Append("</code></pre>")
    sb.Append("<script src=""prism.js""></script>")
    sb.Append("<script src=""prism-b4x.js""></script>")
    sb.Append("<script>")
    sb.Append("Prism.highlightAll();")
    sb.Append("</script>")
    sb.Append("</body>")
    sb.Append("</sb>")
    txtString.Text = sb.tostring




    Dim HTML As String
    HTML = File.ReadString(File.DirApp, "web\index.html")
    WebView1.LoadHtml(HTML)

    
End Sub
 

Attachments

  • play html.rar
    34.2 KB · Views: 52
  • kq.jpg
    kq.jpg
    125.1 KB · Views: 54

MicroDrie

Well-Known Member
Licensed User
In addition to Erel's question, where are the prism.js and prism-b4x.js files?

To answer your question about the broken java script files, you need the WebViewExtras library.

Java script in webview example:
Sub Process_Globals
    Private WebView1 As WebView
    Private WebViewExtras1 As WebViewExtras
End Sub

Sub Activity_Create(FirstTime As Boolean)
    WebView1.Initialize("WebView1")
    Activity.AddView(WebView1, 0, 0, 100%x, 100%y)
    WebViewExtras1.Initialize(WebView1)
    WebViewExtras1.LoadHtml("<html><body><h1 id='myHeading'>Hello World!</h1><button onclick='changeText()'>Click me</button><script>function changeText() {document.getElementById('myHeading').innerText = 'Hello B4A!';}</script></body></html>", "")
End Sub

Sub WebView1_PageFinished (Url As String)
    WebViewExtras1.ExecuteJavascript("alert('Page finished loading.');")
End Sub
 
Upvote 0
Top