Golang html/template.JSEscape function example

package html/template

JSEscape writes to w(1st parameter) the escaped JavaScript equivalent of the plain text data b (2nd parameter).

Golang html/template.JSEscape function usage example

 package main

 import (
 "bytes"
 "fmt"
 "html/template"
 )

 func main() {

 w := bytes.NewBufferString("")

 b := []byte("<script>document.getElementById('demo').innerHTML = cars;</script>")

 template.JSEscape(w, b)

 fmt.Println(w)

 }

Output :

\x3Cscript\x3Edocument.getElementById(\'demo\').innerHTML = cars;\x3C/script\x3E

Reference :

http://golang.org/pkg/html/template/#JSEscape

Advertisement