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

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

Javascript not working in go webserver

问题

main.go

  1. func thandle(rw http.ResponseWriter, req *http.Request) {
  2. t, _ := template.ParseFiles("basic.html")//
  3. t.Execute(rw, nil)
  4. }
  5. func main() {
  6. http.HandleFunc("/", thandle)
  7. http.ListenAndServe(":9999", nil)
  8. }

basic.html

  1. <head>
  2. <script src="js/jquery-1.10.2.min.js"></script>
  3. <script>
  4. $(document).ready(function() {
  5. alert('hey');
  6. });
  7. </script>
  8. </head>

如果你能给我一个正确的代码示例,我会很高兴。

英文:

main.go

  1. func thandle(rw http.ResponseWriter, req *http.Request) {
  2. t, _ := template.ParseFiles(&quot;basic.html&quot;)//
  3. t.Execute(rw, nil)
  4. }
  5. func main() {
  6. http.HandleFunc(&quot;/&quot;, thandle)
  7. http.ListenAndServe(&quot;:9999&quot;, nil)
  8. }

I don't understand.
Which part wrong?

basic.html

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

if you can
show me right code example?

答案1

得分: 2

与Go无关的问题。尝试:

  1. <head>
  2. <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
  3. <script>
  4. $(document).ready(function() {
  5. alert('hey');
  6. });</script>
  7. </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:

  1. &lt;head&gt;
  2. &lt;script src=&quot;http://code.jquery.com/jquery-1.10.1.min.js&quot;&gt;&lt;/script&gt;
  3. &lt;script&gt;
  4. $(document).ready(function() {
  5. alert(&#39;hey&#39;);
  6. });&lt;/script&gt;
  7. &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:

确定