Files
30-seconds-of-code/snippets/redirect.md
Angelos Chalaris 611729214a Snippet format update
To match the starter (for the migration)
2019-08-13 10:29:12 +03:00

18 lines
427 B
Markdown

---
title: redirect
tags: browser,url,beginner
---
Redirects to a specified URL.
Use `window.location.href` or `window.location.replace()` to redirect to `url`.
Pass a second argument to simulate a link click (`true` - default) or an HTTP redirect (`false`).
```js
const redirect = (url, asLink = true) =>
asLink ? (window.location.href = url) : window.location.replace(url);
```
```js
redirect('https://google.com');
```