blob: e679b5f852f27938999996d5e043886536b9dbf9 (
plain) (
blame)
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
|
---
title: safe.JS
linkTitle: safeJS
description: Declares the provided string as a known safe JavaScript string.
categories: [functions]
keywords: []
menu:
docs:
parent: functions
function:
aliases: [safeJS]
returnType: template.JS
signatures: [safe.JS INPUT]
relatedFunctions:
- safe.CSS
- safe.HTML
- safe.HTMLAttr
- safe.JS
- safe.JSStr
- safe.URL
aliases: [/functions/safejs]
---
In this context, *safe* means the string encapsulates a known safe EcmaScript5 Expression (e.g., `(x + y * z())`).
Template authors are responsible for ensuring that typed expressions do not break the intended precedence and that there is no statement/expression ambiguity as when passing an expression like `{ foo:bar() }\n['foo']()`, which is both a valid expression and a valid program with a very different meaning.
Example: Given `hash = "619c16f"` defined in the front matter of your `.md` file:
* <span class="good">`<script>var form_{{ .Params.hash | safeJS }};…</script>` → `<script>var form_619c16f;…</script>`</span>
* <span class="bad">`<script>var form_{{ .Params.hash }};…</script>` → `<script>var form_"619c16f";…</script>`</span>
|