Created
February 7, 2017 13:33
-
-
Save influx6/5ad0923941368928397d30c9cf87c0d8 to your computer and use it in GitHub Desktop.
Add guard to call javascript function from JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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