Skip to content

Commit

Permalink
📝 Add explanations about how variables are evaluated in code
Browse files Browse the repository at this point in the history
Closes #390
  • Loading branch information
baptisteArno committed Mar 23, 2023
1 parent 5090bad commit 69ee590
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 37 deletions.
20 changes: 11 additions & 9 deletions apps/docs/docs/editor/blocks/logic/script.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# 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.

**It doesn't allow you to create a custom visual 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

### Reload page
Expand All @@ -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

```js
postMessage('hello there!', '*')
```

Then on your parent website, you could listen for those messages:
### Redirect if a variable has a specific value

```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)
30 changes: 3 additions & 27 deletions apps/docs/docs/editor/blocks/logic/set-variable.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion apps/docs/docs/embed/script.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down

0 comments on commit 69ee590

Please sign in to comment.