# Run client-side JS in Astro MDX

> Learn how to run client-side JavaScript inside an Astro MDX file, where a plain script tag breaks, by wrapping your code in a template literal.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2024-04-13 | Topics: [Astro](https://flaviocopes.com/tags/astro/) | Canonical: https://flaviocopes.com/run-client-side-js-in-astro-mdx/

I wanted to add some bits of client-side JS in a .mdx file in [Astro](https://flaviocopes.com/tags/astro).

Just a one-liner to redirect after 2 seconds, nothing too crazy:

```javascript
setTimeout(() => (location.href = '/'), 2000)
```

In a `.astro` component I’d normally use:

```javascript
<script>
  setTimeout(() => (location.href = '/'), 2000)
</script>
```

but this didn’t work because in the browser what we get is (due to MDX compiler, escaping, etc):

```javascript
<script><p>setTimeout(() =&gt; (location.href = ’/’), 2000)</p></script>
```

I ended up using this syntax:

```javascript
<script>{`
  setTimeout(() => (location.href = '/'), 2000)
`}</script>
```

and this worked fine.
