# 脚本

{% hint style="info" %}

### 逻辑-脚本组件

{% endhint %}

<figure><img src="/files/QjZsKIBtfsLD8khOqKLK" alt="" width="275"><figcaption></figcaption></figure>

{% hint style="info" %}
“脚本”组件只能执行 Javascript 代码。
{% endhint %}

<figure><img src="/files/UKH9za9MfWiBgD8p1RY8" alt="" width="375"><figcaption></figcaption></figure>

{% hint style="info" %}
注意：

如果你在“脚本”组件中加了双引号，那么脚本中的变量不会被解析。

所以你需要写`console.log({{My variable}})`

而不是写`console.log("{{My variable}}")`
{% endhint %}

### **setVariable功能**

如果你想使用 Javascript 设置变量值，那么建议你去用“[设置变量](/jin-jie-jiao-cheng-gong-zuo-liu/luo-ji-zu-jian/she-zhi-bian-liang.md)”组件。因为在大多数情况下“[设置变量](/jin-jie-jiao-cheng-gong-zuo-liu/luo-ji-zu-jian/she-zhi-bian-liang.md)”组件更合适你的需求。

如果您执意想使用脚本组件设置变量，那么可以使用`setVariable`在脚本中定义变量函数。例如：

```javascript
if({{My variable}} === 'foo') {
  setVariable('My variable', 'bar')
} else {
  setVariable('My variable', 'other')
}
```

注意：`setVariable`功能，只能在服务器上执行的脚本中使用，如果你开启了“在客户端执行”，那么`setVariable`将不会起到任何作用。<br>

### 脚本组件示例

**1、通过变量传递参数**

* 1、在输入本文组件，定义一个变量名：My variable

<figure><img src="/files/rIHHMCTvJ1A94UUWvtCr" alt=""><figcaption></figcaption></figure>

* 2、在脚本组件设置变量名为：My variable

<figure><img src="/files/ETQ02lfXIt6QwhB4klrV" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/9ek3JzCW88qNVAU0GHIR" alt="" width="509"><figcaption></figcaption></figure>

* 3、在气泡组件内输出My variable变量名：

<figure><img src="/files/xa3f0FALHWN9PoZRiUxT" alt=""><figcaption></figcaption></figure>

####

#### 2、重新加载页面

```javascript
window.location.reload()
```

####

#### 3、页面提示

```
alert("这是脚本组件的提示")
```

####

#### 4、如果变量具有特定值则重定向

```javascript
if({{Category}} === 'qualified') {
  window.location.href = 'https://codemossai.com/'
}
```

{% hint style="info" %}
还有很多 Javascript 代码可以执行，这将需要您自行挖掘
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.nyai.chat/jin-jie-jiao-cheng-gong-zuo-liu/luo-ji-zu-jian/jiao-ben.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
