Documentation
¶
Overview ¶
Package script provides constructors and methods for the HTML <script> element.
The <script> HTML element is used to embed executable code or data; this is typically used to embed or refer to JavaScript code. The <script> element can also be used with other languages, such as WebGL's GLSL shader programming language and JSON.
Index ¶
- Variables
- func JSON(data string) *element
- func JavaScript(src string) *element
- func Module(src string) *element
- func New(nodes ...node.Node) *element
- func RawText(content string) *element
- func RawTextf(format string, args ...any) *element
- func Src(src string) *element
- func Static(content string) *element
- func Text(content string) *element
- func Textf(format string, args ...any) *element
- type Element
Constants ¶
This section is empty.
Variables ¶
var ( TagOpen = []byte("<script") TagClose = []byte("</script>") AttrSrc = []byte(" src=\"") AttrType = []byte(" type=\"") AttrAsync = []byte(" async") AttrCrossOrigin = []byte(" crossorigin=\"") AttrDefer = []byte(" defer") AttrIntegrity = []byte(" integrity=\"") AttrNoModule = []byte(" nomodule") AttrNonce = []byte(" nonce=\"") AttrReferrerPolicy = []byte(" referrerpolicy=\"") AttrBlocking = []byte(" blocking=\"") AttrFetchPriority = []byte(" fetchpriority=\"") )
Byte constants for HTML rendering.
Functions ¶
func JSON ¶
func JSON(data string) *element
JSON creates a script element with JSON data Example: script.JSON("{\"key\": \"value\"}") Renders: <script type="application/json">{"key": "value"}</script>
func JavaScript ¶
func JavaScript(src string) *element
JavaScript creates a script element explicitly with JavaScript type Example: script.JavaScript("script.js") Renders: <script src="script.js" type="text/javascript"></script>
func Module ¶
func Module(src string) *element
Module creates a new script element with type="module" for ES6 modules Example: script.Module("app.js") Renders: <script src="app.js" type="module"></script>
func New ¶
New creates a new script element without any initial attributes Example: script.New() Renders: <script></script>
func RawText ¶
func RawText(content string) *element
RawText creates a new script element with raw inline JavaScript. Uses text.RawText which is not HTML-escaped. Preferred for dynamic inline scripts. Example: script.RawText("var x = 1;") Renders: <script>var x = 1;</script>
func RawTextf ¶
RawTextf creates a new script element with formatted raw inline JavaScript. Uses text.RawTextf which is not HTML-escaped. Preferred for dynamic inline scripts with interpolation. Example: script.RawTextf("var %s = %d;", varName, val) Renders: <script>var x = 42;</script>
func Src ¶ added in v0.2.4
func Src(src string) *element
Src creates a script element that loads an external script file Example: script.Src("/assets/js/app.js") Renders: <script src="/assets/js/app.js"></script>
func Static ¶
func Static(content string) *element
Static creates a new script element with static inline JavaScript. Uses text.Static which is not HTML-escaped and is JIT-optimisable. Preferred for compile-time constant scripts. Example: script.Static("alert('Hi');") Renders: <script>alert('Hi');</script>
func Text ¶
func Text(content string) *element
Text creates a new script element with text content. Uses text.Text which HTML-escapes the output. Warning: HTML escaping will break JavaScript syntax. Use RawText or Static for inline scripts. Example: script.Text("console.log('Hello');") Renders: <script>console.log('Hello');</script>
func Textf ¶
Textf creates a new script element with formatted text content. Uses text.Textf which HTML-escapes the output. Warning: HTML escaping will break JavaScript syntax. Use RawTextf for inline scripts. Example: script.Textf("console.log('%s');", msg) Renders: <script>console.log('hello');</script>