在Go Web服务器中,JavaScript不起作用。

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

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(&quot;basic.html&quot;)// 
	t.Execute(rw, nil)
}

func main() {
	http.HandleFunc(&quot;/&quot;, thandle)
	http.ListenAndServe(&quot;:9999&quot;, nil)
}

I don't understand.
Which part wrong?

basic.html

&lt;head&gt;
&lt;script src=&quot;js/jquery-1.10.2.min.js&quot;&gt;&lt;/script&gt;
 &lt;script&gt;
	 $(document).ready(function() {
	 	 alert(&#39;hey&#39;);
	 });&lt;/script&gt;
&lt;/head&gt;

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>

你需要了解客户端和服务器端编程的区别。也许这些链接可以帮助你:

  1. https://softwareengineering.stackexchange.com/questions/171203/what-are-the-difference-between-server-side-and-client-side-programming
  2. 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:

&lt;head&gt;
&lt;script src=&quot;http://code.jquery.com/jquery-1.10.1.min.js&quot;&gt;&lt;/script&gt;
 &lt;script&gt;
     $(document).ready(function() {
         alert(&#39;hey&#39;);
     });&lt;/script&gt;
&lt;/head&gt;

You need to understand what is the different beetwen client-side and server-side programming. Maybe these link will help you:

  1. https://softwareengineering.stackexchange.com/questions/171203/what-are-the-difference-between-server-side-and-client-side-programming
  2. 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

huangapple
  • 本文由 发表于 2014年1月26日 22:25:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/21364564.html
匿名

发表评论

匿名网友

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

确定