問題:我的腳本src標記在我的index.html頁面中,但在瀏覽器上,此腳本未加載(實際上它加載但看起來像index.html文件0_0看下面的圖片)。問題是:我如何使用html代碼中的不同文件(myscripts.js)中的JavaScript代碼?轉到如何在我的html頁面上使用JavaScript代碼
js file looks like html (index.html) file
在谷歌上搜索,我發現「解決方案」,但我並不像它應該完全工作。我只是說此行的ViewHandler方法:
http.ServeFile(w, r, r.URL.Path[1:])
在此之後,項目的啓動是這樣的:
index.html after adding line above
綜上所述:
我的目標是顯示HTML頁面並使用腳本/文件夾中的JavaScript函數。
項目結構:
-scripts
--myscripts.js
-templates
--index.html
server.go
Server.go:
func viewHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("listening . . .")
tpl.ExecuteTemplate(w, "index.html", nil)
}
func main() {
http.HandleFunc("/", viewHandler)
http.ListenAndServe(":8080", nil)
}
myscripts.js:
function clickFunc(){
console.log("clicked")
}
周的index.html:
<head>
<script type="text/javascript" src="scripts/myscripts.js"></script>
</head>
<body>
<button onclick="clickFunc()">Click me</button>
</body>
</html>
rofl,「http.ServeFile(w,r,r.URL.Path [1:])」 - 記憶中,作爲白癡代碼的一個例子。小,你一定會陷入困境。示例我的代碼:https://github.com/spouk/spoukfw/blob/master/spoukrender.go – Spouk