英文:
Javascript not working in go webserver
问题
main.go
func thandle(rw http.ResponseWriter, req *http.Request) {
t, _ := template.ParseFiles("basic.html")//
t.Execute(rw, nil)
}
func main() {
http.HandleFunc("/", thandle)
http.ListenAndServe(":9999", nil)
}
basic.html
<head>
<script src="js/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function() {
alert('hey');
});
</script>
</head>
如果你能给我一个正确的代码示例,我会很高兴。
英文:
main.go
func thandle(rw http.ResponseWriter, req *http.Request) {
t, _ := template.ParseFiles("basic.html")//
t.Execute(rw, nil)
}
func main() {
http.HandleFunc("/", thandle)
http.ListenAndServe(":9999", nil)
}
I don't understand.
Which part wrong?
basic.html
<head>
<script src="js/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function() {
alert('hey');
});</script>
</head>
if you can
show me right code example?
答案1
得分: 2
与Go无关的问题。尝试:
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function() {
alert('hey');
});</script>
</head>
你需要了解客户端和服务器端编程的区别。也许这些链接可以帮助你:
- https://softwareengineering.stackexchange.com/questions/171203/what-are-the-difference-between-server-side-and-client-side-programming
- http://openmymind.net/2012/5/30/Client-Side-vs-Server-Side-Rendering/
如果你想托管自己的js文件(或其他静态文件),你可以使用相对URL(无需更改你的HTML代码),参见http://golang.org/pkg/net/http/#example_FileServer_stripPrefix
英文:
The issue not related to Go. Try:
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function() {
alert('hey');
});</script>
</head>
You need to understand what is the different beetwen client-side and server-side programming. Maybe these link will help you:
- https://softwareengineering.stackexchange.com/questions/171203/what-are-the-difference-between-server-side-and-client-side-programming
- http://openmymind.net/2012/5/30/Client-Side-vs-Server-Side-Rendering/
If you want to host your own js file (an other static file) so you can use realtive url (don't need to change your HTML code) see http://golang.org/pkg/net/http/#example_FileServer_stripPrefix
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论