# JavaScript Widget

With Userdesk you can embed a JavaScript Widget on your website, and a chat bubble will appear on your website.

This widget exposes a global JavaScript object called `Userdesk` and you can execute some advanced actions.

## Set the user name

Set the current user's name, which is useful if the person is logged in to your website, and you want to see the name in the **Messages** section.

```javascript
Userdesk.setName('John Doe')
```

## Set user email

Set the current user's email, which is useful if the person is logged in to your website, and you want to see the email in the Messages section and be able to send them emails when they are not online.

```javascript
Userdesk.setEmail('john@doe.com')
```

## Open the widget

```javascript
Userdesk.openWidget()
```

## Close the widget

```javascript
Userdesk.closeWidget()
```

## Hide the widget

Completely hides the widget from the page.

```javascript
Userdesk.hideWidget()
```

## Show the widget

```javascript
Userdesk.showWidget()
```

## Set dark mode

It programmatically switches the widget to dark mode. It is useful if you have a dark mode selector on your website.\
Otherwise, the widget automatically takes the color mode of the user system.

```javascript
Userdesk.setDarkMode()
```

## Set light mode

```javascript
Userdesk.setLightMode()
```

## Escalate to human callback

Userdesk allows you to define an Escalate to Human button, via the Widget settings. It also provides a flow to ask for user details and receive an email.

But you can also define a custom callback to execute your JavaScript code when the user clicks the button.

For instance, you could close the widget, and open your own contact details form.

To define your callback use:

```javascript
Userdesk.onEscalateToHuman = () => {
    ...your code...
}
```

## Ask questions on behalf of the user

```javascript
Userdesk.askUserQuestion("question")
```
