Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

263
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda