Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "html/template"
- "net/http"
- )
- var tpl *template.Template
- func init() {
- tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
- }
- func main() {
- http.HandleFunc("/", index)
- http.HandleFunc("/one", one)
- http.ListenAndServe(":8080", nil)
- }
- func index(w http.ResponseWriter, req *http.Request) {
- tpl.ExecuteTemplate(w, "index.gohtml", nil)
- }
- func one(w http.ResponseWriter, req *http.Request) {
- s := "This is one.gohtml"
- fmt.Fprintln(w, s)
- }
- =============================================================
- <!DOCTYPE html>
- <html>
- <head>
- <title>Index</title>
- </head>
- <body>
- <h3>Alert!</h3>
- <h4>Make something to happen</h4>
- <script type="text/javascript">
- document.querySelector('h3').onclick = makeAlert;
- document.querySelector('h4').onclick = makeText;
- function makeAlert(){
- var xhr = new XMLHttpRequest();
- xhr.open('GET', '/one', true);
- xhr.onreadystatechange = function(){
- if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200){
- alert(xhr.responseText);
- }
- };
- xhr.send();
- }
- function makeText(){
- var xhr = new XMLHttpRequest();
- xhr.open('GET','/one',true);
- xhr.onreadystatechange = function(){
- if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200){
- var b = document.querySelector('body');
- var myHeadingOne = document.createElement('h5');
- var myHeadingOneText = document.createTextNode(xhr.responseText);
- myHeadingOne.appendChild(myHeadingOneText);
- b.appendChild(myHeadingOne);
- }
- };
- xhr.send()
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment