krovn

Untitled

Oct 28th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "html/template"
  6.     "net/http"
  7. )
  8.  
  9. var tpl *template.Template
  10.  
  11. func init() {
  12.     tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
  13. }
  14.  
  15. func main() {
  16.     http.HandleFunc("/", index)
  17.     http.HandleFunc("/one", one)
  18.     http.ListenAndServe(":8080", nil)
  19. }
  20.  
  21. func index(w http.ResponseWriter, req *http.Request) {
  22.     tpl.ExecuteTemplate(w, "index.gohtml", nil)
  23. }
  24.  
  25. func one(w http.ResponseWriter, req *http.Request) {
  26.     s := "This is one.gohtml"
  27.     fmt.Fprintln(w, s)
  28. }
  29. =============================================================
  30. <!DOCTYPE html>
  31. <html>
  32. <head>
  33.   <title>Index</title>
  34. </head>
  35. <body>
  36.   <h3>Alert!</h3>
  37.   <h4>Make something to happen</h4>
  38.   <script type="text/javascript">
  39.     document.querySelector('h3').onclick = makeAlert;
  40.     document.querySelector('h4').onclick = makeText;
  41.     function makeAlert(){
  42.       var xhr = new XMLHttpRequest();
  43.       xhr.open('GET', '/one', true);
  44.       xhr.onreadystatechange = function(){
  45.         if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200){
  46.           alert(xhr.responseText);
  47.         }
  48.       };
  49.       xhr.send();
  50.     }
  51.     function makeText(){
  52.       var xhr = new XMLHttpRequest();
  53.       xhr.open('GET','/one',true);
  54.       xhr.onreadystatechange = function(){
  55.         if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200){
  56.           var b = document.querySelector('body');
  57.           var myHeadingOne = document.createElement('h5');
  58.           var myHeadingOneText = document.createTextNode(xhr.responseText);
  59.           myHeadingOne.appendChild(myHeadingOneText);
  60.           b.appendChild(myHeadingOne);
  61.         }
  62.       };
  63.       xhr.send()
  64.     }
  65.   </script>
  66. </body>
  67. </html>
Advertisement
Add Comment
Please, Sign In to add comment