Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a redirect with HTMX?

HTMX is great and using AJAX for most of the stuff is awesome! But from time to time I just need a real "phyiscal" redirect to another page.

Any ideas how to achieve this without starting to write custom JS code?

like image 654
Ron Avatar asked Feb 03 '26 23:02

Ron


1 Answers

Luckily, after googleing for ages I found this post at Reddit: https://www.reddit.com/r/htmx/comments/ot6kai/comment/h6v5cn9/?utm_source=share&utm_medium=web2x&context=3

You can easily set the HX-Redirect header in your backend and on the return of the response, your browser will magically redirect.

The docs state this here but unfortunately don't explain it in more detail on other pages.

Supplemantal: I experienced an issue with JS errors occuring after the redirect - even though everything works fine.

like image 153
Ron Avatar answered Feb 05 '26 12:02

Ron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!