Skip to content

Instantly share code, notes, and snippets.

@influx6
Created February 7, 2017 13:33
Show Gist options
  • Save influx6/5ad0923941368928397d30c9cf87c0d8 to your computer and use it in GitHub Desktop.
Save influx6/5ad0923941368928397d30c9cf87c0d8 to your computer and use it in GitHub Desktop.
Add guard to call javascript function from JS
package main
import (
"fmt"
"github.com/gopherjs/gopherjs/js"
)
var script = `
window.Guard = function(caller){
try {
return caller.apply(this, [].slice.call(arguments,1))
}catch(e){
return e
}
}
`
func main() {
fmt.Println("Hello, playground")
fmt.Println("Hello, JS console")
scriptObj := js.Global.Get("document").Call("createElement", "script")
scriptObj.Set("textContent", script)
js.Global.Get("document").Get("body").Call("appendChild", scriptObj)
js.Global.Call("Guard", func(name string) { js.Global.Get("console").Call("log", "Name:", name) }, "dude")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment