📝 Add explanations about how variables are evaluated in code
Closes #390
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
# Script
|
# Script block
|
||||||
|
|
||||||
The "Script" block allows you to execute Javascript code. If you want to set a variable value with Javascript, use the [Set variable block](./set-variable) instead.
|
The "Script" block allows you to execute Javascript code. If you want to set a variable value with Javascript, use the [Set variable block](./set-variable) instead.
|
||||||
|
|
||||||
@@ -6,6 +6,12 @@ The "Script" block allows you to execute Javascript code. If you want to set a v
|
|||||||
|
|
||||||
<img src="/img/blocks/logic/code.png" width="600" alt="Code block"/>
|
<img src="/img/blocks/logic/code.png" width="600" alt="Code block"/>
|
||||||
|
|
||||||
|
:::note
|
||||||
|
Variables in script are not parsed, they are evaluated. So it should be treated as if it were real javascript variables.
|
||||||
|
|
||||||
|
You need to write `console.log({{My variable}})` instead of `console.log("{{My variable}}")`
|
||||||
|
:::
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
### Reload page
|
### Reload page
|
||||||
@@ -14,16 +20,12 @@ The "Script" block allows you to execute Javascript code. If you want to set a v
|
|||||||
window.location.reload()
|
window.location.reload()
|
||||||
```
|
```
|
||||||
|
|
||||||
### Post a message to parent
|
### Redirect if a variable has a specific value
|
||||||
|
|
||||||
```js
|
```js
|
||||||
postMessage('hello there!', '*')
|
if({{Category}} === 'qualified') {
|
||||||
```
|
window.location.href = 'https://my-site.com'
|
||||||
|
}
|
||||||
Then on your parent website, you could listen for those messages:
|
|
||||||
|
|
||||||
```js
|
|
||||||
addEventListener('message', ({ data }) => console.log(data))
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Do you need to do something but you're not sure how to? [Ask the community for help!](https://www.facebook.com/groups/typebot)
|
Do you need to do something but you're not sure how to? [Ask the community for help!](https://www.facebook.com/groups/typebot)
|
||||||
|
|||||||
@@ -83,32 +83,8 @@ Or a random ID:
|
|||||||
Math.round(Math.random() * 1000000)
|
Math.round(Math.random() * 1000000)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Current URL
|
:::note
|
||||||
|
Keep in mind that the code is executed on the server. So you don't have access to browser variables such as `window` or `document`.
|
||||||
A popular request also is to set a variable to the current URL. Here is the value that should be inserted:
|
|
||||||
|
|
||||||
```js
|
|
||||||
window.location.href
|
|
||||||
```
|
|
||||||
|
|
||||||
:::caution
|
|
||||||
It will not give you the parent URL if you embed the bot on your site.
|
|
||||||
A more bulletproof option is to pass the URL as a prefilled variable in the embed code options. You can find an example [here](/embed/html-javascript#additional-configuration).
|
|
||||||
:::
|
:::
|
||||||
|
|
||||||
## Extract a cookie
|
The code can also be multi-line. The Set variable block will get the value following the `return` statement.
|
||||||
|
|
||||||
This code allows you to extract the value of a cookie called "my_cookie":
|
|
||||||
|
|
||||||
```js
|
|
||||||
const getCookie = (name) => {
|
|
||||||
const value = `; ${document.cookie}`
|
|
||||||
const parts = value.split(`; ${name}=`)
|
|
||||||
if (parts.length === 2) return parts.pop().split(';').shift()
|
|
||||||
return 'not found'
|
|
||||||
}
|
|
||||||
|
|
||||||
return getCookie('my_cookie')
|
|
||||||
```
|
|
||||||
|
|
||||||
As you can see the code can also be multi-line. The Set variable block will get the value following the `return` statement.
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
# Script
|
# Script embed snippet
|
||||||
|
|
||||||
The script embed option is useful only if you don't have access to the HTML tree of your application or if your website builder only allows you to inline script snippets.
|
The script embed option is useful only if you don't have access to the HTML tree of your application or if your website builder only allows you to inline script snippets.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user