Replies: 2 comments 4 replies
-
I'm surprised no-one could help with this. After digging into this I would like to present a more elegant solution: HTMX adds additional headers to HTTP request headers. You can inspect these and tailor your response. In particular the "HX-Current-URL" is of interest here. If you don't serve from or have access to your web server directly and interact with and/or have access to an endpointyou can read the request url from this additional header HTMX appends:
And you can then extract the query string, in this example "id":
And then append the "id" to your fetch call, e.g.:
This way on page load you can feed the query string variables from your current URL into a fetch call to any API.
Hope this helps someone coming across the same problem. |
Beta Was this translation helpful? Give feedback.
-
Hey,
Yep, that's exactly it! I'm not sure I understand the situation here, more precisely ; why can't you do this on the backend? It seems convoluted indeed to use a I know you said "If you don't serve from or have access to your web server directly" but I'd be curious to know more about that, what's the situation exactly? |
Beta Was this translation helpful? Give feedback.
-
Hi all. Very simple landing page, thought it would be a good place to start playing with HTMX. Forgive me if I ask sth. very stupid. The objective is really simple:
Landing page with a query string in the URL. onLoad it fires a request to server, sending the params from the query string along. Server does its thing and sends back a success or error HTML snippet.
I do not need a button or form, all the info I need comes from the queryString. Users get redireted to this page from another app.
Question 1: How can I get the searchParms into the hx-get?
I must be missing something but I couldn't find any other way than using some JS.
HTMX:
JS:
Sample:
https://stackblitz.com/edit/stackblitz-starters-9z2kgg?file=index.html
Question 2; I would have thought the entire idea behind HTMX was that for the very largest part I don't need to write the JS.
Or is this a case where I would look at Hyperscript?
Question 3: hx-trigger="load" on a div looks odd to me, surely that's not eactly how it's thought to be done?
In production I would of course do the entire parsing of the queryString and writing the HTML response in the backend - but for that I need to get the params from the queryString back there first. What am I missing?
Thanks :)
Beta Was this translation helpful? Give feedback.
All reactions