📝 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.
|
||||
|
||||
@@ -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"/>
|
||||
|
||||
:::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
|
||||
|
||||
### 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()
|
||||
```
|
||||
|
||||
### Post a message to parent
|
||||
### Redirect if a variable has a specific value
|
||||
|
||||
```js
|
||||
postMessage('hello there!', '*')
|
||||
```
|
||||
|
||||
Then on your parent website, you could listen for those messages:
|
||||
|
||||
```js
|
||||
addEventListener('message', ({ data }) => console.log(data))
|
||||
if({{Category}} === 'qualified') {
|
||||
window.location.href = 'https://my-site.com'
|
||||
}
|
||||
```
|
||||
|
||||
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)
|
||||
```
|
||||
|
||||
## Current URL
|
||||
|
||||
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).
|
||||
:::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`.
|
||||
:::
|
||||
|
||||
## Extract a cookie
|
||||
|
||||
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.
|
||||
The code can also be multi-line. The Set variable block will get the value following the `return` statement.
|
||||
|
||||
Reference in New Issue
Block a user