├── README.md ├── LICENSE.txt └── onload.go /README.md: -------------------------------------------------------------------------------- 1 | [![GoDoc](https://godoc.org/github.com/flimzy/onload?status.png)](http://godoc.org/github.com/flimzy/onload) 2 | 3 | **onload** is a very simple module, designed to provide cross-browser, jQuery-like onload 4 | functionality without the bloat of jQuery. It is writen entirely in Go. To use it: 5 | 6 | package main 7 | 8 | import ( 9 | "fmt" 10 | "github.com/flimzy/onload" 11 | ) 12 | 13 | func main() { 14 | onload.Ready( func() { 15 | fmt.Print("The document is ready!\n") 16 | }) 17 | } 18 | 19 | This code is based on [this StackOverflow answer](http://stackoverflow.com/a/9899701/13860), 20 | but somewhat simplified, and of course adapted to Go. 21 | 22 | This software is released under the terms of the MIT license. See LICENSE.txt for details. 23 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Jonathan Hall 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /onload.go: -------------------------------------------------------------------------------- 1 | // +build js 2 | 3 | // Package onload provides a minimal, cross-browser document onload trigger 4 | // for GopherJS. It is adapted from http://stackoverflow.com/a/9899701/13860 5 | package onload 6 | 7 | import "github.com/gopherjs/gopherjs/js" 8 | 9 | var readyList []func() = make([]func(), 0, 1) 10 | var readyFired bool = false 11 | var readyEventHandlersInstalled bool = false 12 | 13 | // Ready registers a function to fire when the page is loaded. This is effectively 14 | // the same as jQuery's $.ready(), but written in Go, and without the bloat of jQuery. 15 | // Ready() may be called multiple times, and once the document is ready, registered 16 | // functions will be called in registration order. 17 | func Ready(fn func()) { 18 | if readyFired { 19 | go fn() 20 | return 21 | } 22 | readyList = append(readyList, fn) 23 | doc := js.Global.Get("document") 24 | if doc.Get("readyState").String() == "complete" { 25 | go ready() 26 | return 27 | } 28 | if !readyEventHandlersInstalled { 29 | if doc.Get("addEventListener") != js.Undefined { 30 | // first choice is DOMContentLoaded event 31 | doc.Call("addEventListener", "DOMContentLoaded", ready, false) 32 | // backup is window load event 33 | js.Global.Call("addEventListener", "load", ready, false) 34 | } else { 35 | // Must be IE 36 | doc.Call("attachEvent", "onreadystatechange", readyStatechange) 37 | js.Global.Call("attachEvent", "onload", ready) 38 | } 39 | readyEventHandlersInstalled = true 40 | } 41 | } 42 | 43 | // ready does the actual work of running the registered functions when the 44 | // document is finally ready 45 | func ready() { 46 | if !readyFired { 47 | readyFired = true 48 | for _, readyFunc := range readyList { 49 | readyFunc() 50 | } 51 | readyList = nil 52 | } 53 | } 54 | 55 | // readyStateChange is a ready() wrapper for IE 56 | func readyStatechange() { 57 | if js.Global.Get("document").Get("readyState").String() == "complete" { 58 | ready() 59 | } 60 | } 61 | --------------------------------------------------------------------------------