-
Notifications
You must be signed in to change notification settings - Fork 6
/
javascript.go
79 lines (72 loc) · 2.04 KB
/
javascript.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package onthefly
import "errors"
// Link a page up with a JS file
// Takes the url to a JS file as a string
// The given page must have a "head" tag for this to work
// Returns an error if no "head" tag is found, or nil
func (page *Page) LinkToJS(jsURL string) error {
_, err := page.LinkToJSInHead(jsURL)
return err
}
// Link to JavaScript in the head
func (page *Page) LinkToJSInHead(jsURL string) (*Tag, error) {
head, err := page.GetTag("head")
if err != nil {
return nil, err
}
script := head.AddNewTag("script")
script.AddAttrib("src", jsURL)
script.AddAttrib("type", "text/javascript")
script.AddContent(" ")
return script, nil
}
// Link to JavaScript, at the end of the body
func (page *Page) LinkToJSInBody(jsURL string) (*Tag, error) {
body, err := page.GetTag("body")
if err != nil {
return nil, err
}
script := body.AddNewTag("script")
script.AddAttrib("src", jsURL)
script.AddAttrib("type", "text/javascript")
script.AddContent(" ")
return script, nil
}
// Add JavaScript code in a script tag in the head tag
// Deprecated, use AddScriptToHead instead.
func AddScriptToHeader(page *Page, js string) error {
_, err := page.AddScriptToHead(js)
return err
}
// Add javascript code in a script tag in the head tag
func (page *Page) AddScriptToHead(js string) (*Tag, error) {
// Check if there's anything to add
if js == "" {
return nil, errors.New("no javascript to add")
}
// Add a script tag
head, err := page.GetTag("head")
if err != nil {
return nil, err
}
script := head.AddNewTag("script")
script.AddAttrib("type", "text/javascript")
script.AddContent(js)
return script, nil
}
// Add javascript code in a script tag at the end of the body tag
func (page *Page) AddScriptToBody(js string) (*Tag, error) {
// Check if there's anything to add
if js == "" {
return nil, errors.New("no javascript to add")
}
// Add a script tag
body, err := page.GetTag("body")
if err != nil {
return nil, err
}
script := body.AddNewTag("script")
script.AddAttrib("type", "text/javascript")
script.AddContent(js)
return script, nil
}