Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

255
Views
Javascript functions not working in webview window

I am new to the web frontend approach. Hence, when you need more information please ask.

I have an app with a go backend and a html/css/js frontend, which works rather nicely. Recently, I attempted to transition to webview, which also worked fine with respect to the html and css part. However, my javascript functions do not work and I am quite at a loss as to how to troubleshoot the problem.

Here is a self contained, minimal example, which demonstrates the issue. The webview window opens and displays the title and the button. However clicking the button will not open the alert dialog.

Any hints pointing me in the right direction are highly appreciated.

package main

import (
    "github.com/webview/webview"
    "html/template"
    "net/http"
)

type DummyData struct {
     PageTitle string
}

func main() {
    go start()
    w := webview.New(true)
    defer w.Destroy()
    w.SetTitle("Test webview")
    w.SetSize(800, 600, webview.HintNone)
    w.Navigate("http://localhost:80")
    w.Run()
}

func start() {
    tmpl, _ := template.New("lala").Parse("<!DOCTYPE html><body><h1>{{.PageTitle}}</h1><button onclick=\"myFunction()\">Try it</button><script>function myFunction() {alert(\"js working\");}</script></body>\n</html>")
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        data := DummyData{
            PageTitle: "My webview page title",
        }
        tmpl.Execute(w, data)
    })
    http.ListenAndServe(":80", nil)
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Actually your JS functions do work, just the alert doesn't. Not exactly sure why, but I would supect that alert, confirm and prompt won't get much love anymore.

This here works:

package main

import (
    "html/template"
    "net/http"

    "github.com/webview/webview"
)

type DummyData struct {
    PageTitle string
}

func main() {
    go start()
    w := webview.New(true)
    defer w.Destroy()
    w.SetTitle("Test webview")
    w.SetSize(800, 600, webview.HintNone)
    w.Navigate("http://localhost:8000")
    w.Run()
}

func start() {
    const page = `
    <!DOCTYPE html>
    <body>
        <h1>
            {{.PageTitle}}
        </h1>
        <button onclick="myFunction()">Try it</button>
    
        <div id="d1" hidden>
            poor man's alert
        </div>
        <script>
            function myFunction() {
                d1.hidden = !d1.hidden
                // alert("js working");
            }
        </script>
    </body>
    </html>
    `
    tmpl, _ := template.New("lala").Parse(page)
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        data := DummyData{
            PageTitle: "My webview page title",
        }
        tmpl.Execute(w, data)
    })
    http.ListenAndServe(":8000", nil)
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!