HTMX HX-Redirect can be used to do a client-side redirect to a new location ¶
Description ¶
New in version 0.4.0: htmx 0.4.0 (2020-11-16)
Can be used to do a client-side redirect to a new location.
Examples ¶
open the pdf in a new tab? yes exactly that ¶
-
https://discord.com/channels/725789699527933952/864934037381971988/900016929861935105
open the pdf in a new tab? yes exactly that. I will try to come up with an example since the whole code is way too big
1cg — Hier à 15:45 OK, what you probably want to do is respond with a HX-Redirect response header to the location of the generated PDF. That should open the PDF in a new tab.
htmx.js source code ¶
1 function handleAjaxResponse(elt, responseInfo) {
2 var xhr = responseInfo.xhr;
3 var target = responseInfo.target;
4
5 if (!triggerEvent(elt, 'htmx:beforeOnLoad', responseInfo)) return;
6
7 if (hasHeader(xhr, /HX-Trigger:/i)) {
8 handleTrigger(xhr, "HX-Trigger", elt);
9 }
10
11 if (hasHeader(xhr,/HX-Push:/i)) {
12 var pushedUrl = xhr.getResponseHeader("HX-Push");
13 }
14
15 if (hasHeader(xhr, /HX-Redirect:/i)) {
16 window.location.href = xhr.getResponseHeader("HX-Redirect");
17 return;
18 }
19
20 if (hasHeader(xhr,/HX-Refresh:/i)) {
21 if ("true" === xhr.getResponseHeader("HX-Refresh")) {
22 location.reload();
23 return;
24 }
25 }