Soy nuevo en el enfoque de interfaz web. Por lo tanto, cuando necesite más información, por favor pregunte.
Tengo una aplicación con un backend go y un frontend html/css/js, que funciona bastante bien. Recientemente, intenté hacer la transición a webview , que también funcionó bien con respecto a la parte html y css. Sin embargo, mis funciones de javascript no funcionan y no sé cómo solucionar el problema.
Aquí hay un ejemplo mínimo e independiente que demuestra el problema. La ventana de vista web se abre y muestra el título y el botón. Sin embargo, hacer clic en el botón no abrirá el cuadro de diálogo de alerta.
Cualquier sugerencia que me indique en la dirección correcta es muy apreciada.
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) }En realidad, sus funciones JS funcionan, solo la alerta no. No estoy exactamente seguro de por qué, pero supongo que alertar, confirmar y avisar ya no recibirá mucho amor.
Esto aquí funciona:
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) }