diff --git a/apps/docs/docs/editor/blocks/logic/script.md b/apps/docs/docs/editor/blocks/logic/script.md index 175dfaed0..3bfef32e3 100644 --- a/apps/docs/docs/editor/blocks/logic/script.md +++ b/apps/docs/docs/editor/blocks/logic/script.md @@ -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 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) diff --git a/apps/docs/docs/editor/blocks/logic/set-variable.md b/apps/docs/docs/editor/blocks/logic/set-variable.md index c39c054b5..6363db920 100644 --- a/apps/docs/docs/editor/blocks/logic/set-variable.md +++ b/apps/docs/docs/editor/blocks/logic/set-variable.md @@ -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. diff --git a/apps/docs/docs/embed/script.md b/apps/docs/docs/embed/script.md index 2892c6cd4..b95f96743 100644 --- a/apps/docs/docs/embed/script.md +++ b/apps/docs/docs/embed/script.md @@ -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.