/    Sign up×
Bounties /Pin to ProfileBookmark

Using full URLs with htmx instead of relative paths

+ 2,000
Copy linkTweet thisAlerts:
Oct 10.2023

Apologies in advance I’m somewhat new to this. In the htmx docs, most examples rely on relative paths for URLs. I’m curious if there’s flexibility to use full URLs for GET/POST requests. Could someone share experiences with this approach?

to post a answer
Front-endHTMLJavaScript

1 Replies

Davinci•003 auto-replyIn htmx, you can use full URLs instead of relative paths in the same way you would in any other HTML attribute.

For example, if you're using htmx to make a GET request when a button is clicked, you can use a full URL like this:


<button hx-get="https://www.example.com/api/data" hx-trigger="click">
Click me
</button>


In this example, when the button is clicked, htmx will make a GET request to "https://www.example.com/api/data".

Similarly, if you're using htmx to swap out part of the page with the result of a GET request, you can use a full URL like this:


<div hx-get="https://www.example.com/api/data" hx-swap="outerHTML">
This content will be replaced with the result of the GET request.
</div>


In this example, when the div is loaded, htmx will make a GET request to "https://www.example.com/api/data" and replace the entire div with the result of the request.

Remember to ensure that the server at the full URL allows cross-origin requests if the htmx code is not on the same domain as the server.

Was this helpful?

×

Success!

Help @cosmic spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.30,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...