在Webview窗口中,JavaScript函数无法正常工作。

huangapple go评论85阅读模式
英文:

Javascript functions not working in webview window

问题

我是你的中文翻译助手,以下是翻译好的内容:

我对Web前端方法还不熟悉。因此,如果你需要更多信息,请提问。

我有一个带有Go后端和HTML/CSS/JS前端的应用程序,工作得相当不错。最近,我尝试过过渡到webview,关于HTML和CSS部分也运行正常。然而,我的JavaScript函数不起作用,我对如何排除问题感到困惑。

这是一个自包含的最小示例,演示了这个问题。Webview窗口打开并显示标题和按钮。然而,点击按钮不会打开警告对话框。

非常感谢任何指向正确方向的提示。

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)
}
英文:

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 (
    &quot;github.com/webview/webview&quot;
    &quot;html/template&quot;
    &quot;net/http&quot;
)

type DummyData struct {
     PageTitle string
}

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

func start() {
    tmpl, _ := template.New(&quot;lala&quot;).Parse(&quot;&lt;!DOCTYPE html&gt;&lt;body&gt;&lt;h1&gt;{{.PageTitle}}&lt;/h1&gt;&lt;button onclick=\&quot;myFunction()\&quot;&gt;Try it&lt;/button&gt;&lt;script&gt;function myFunction() {alert(\&quot;js working\&quot;);}&lt;/script&gt;&lt;/body&gt;\n&lt;/html&gt;&quot;)
    http.HandleFunc(&quot;/&quot;, func(w http.ResponseWriter, r *http.Request) {
	    data := DummyData{
	    	PageTitle: &quot;My webview page title&quot;,
	    }
	    tmpl.Execute(w, data)
    })
    http.ListenAndServe(&quot;:80&quot;, nil)
}

答案1

得分: 1

实际上,你的JS函数是有效的,只是警告框不起作用。不太确定原因,但我怀疑alert、confirm和prompt不再受到重视。

这里的代码是有效的:

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)
}
英文:

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 (
	&quot;html/template&quot;
	&quot;net/http&quot;

	&quot;github.com/webview/webview&quot;
)

type DummyData struct {
	PageTitle string
}

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

func start() {
	const page = `
	&lt;!DOCTYPE html&gt;
	&lt;body&gt;
		&lt;h1&gt;
			{{.PageTitle}}
		&lt;/h1&gt;
		&lt;button onclick=&quot;myFunction()&quot;&gt;Try it&lt;/button&gt;
	
		&lt;div id=&quot;d1&quot; hidden&gt;
			poor man&#39;s alert
		&lt;/div&gt;
		&lt;script&gt;
			function myFunction() {
				d1.hidden = !d1.hidden
				// alert(&quot;js working&quot;);
			}
		&lt;/script&gt;
	&lt;/body&gt;
	&lt;/html&gt;
	`
	tmpl, _ := template.New(&quot;lala&quot;).Parse(page)
	http.HandleFunc(&quot;/&quot;, func(w http.ResponseWriter, r *http.Request) {
		data := DummyData{
			PageTitle: &quot;My webview page title&quot;,
		}
		tmpl.Execute(w, data)
	})
	http.ListenAndServe(&quot;:8000&quot;, nil)
}

huangapple
  • 本文由 发表于 2022年2月12日 21:15:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/71092180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定