What is Htmx? How can Htmx simplify your website?
The web development community has been buzzing about htmx lately, but what is this exciting new technology? It turns out it's much simpler than you might expect, but the usefulness of htmx is enormous.
The web development community has been buzzing about htmx lately, but what is this exciting new technology? It turns out it's much simpler than you might expect, but the usefulness of htmx is enormous.
What is htmx?
htmx is a small JavaScript library with a narrow focus. It provides common JavaScript functionality through HTML attributes. Here is a simple example:
About
This code displays the custom HTML attribute, hx-get. If you click this link, the htmx library will send an AJAX request and load the target page without refreshing the entire browser.
htmx has additional functionality that allows you to send requests:
- From elements other than a and form.
- On events other than click and submit.
- Use HTTP methods other than GET and POST.
- Replace any part of the page instead of just the whole thing.
Although htmx does support technologies like CSS animations and WebSockets, its core purpose is to simplify HTTP request handling.
How you can use htmx in a simple web application
htmx addresses specific functionality within a web application or website, rather than the operation of the entire application.
A big advantage of the library is that it is very easy to get started. You can download and install a copy if you want, but since it has no dependencies, all you need to do to get started is add the link to the script on the CDN:
For local development and testing, you will need to set up a web server, like Apache, if you don't already have one running. This is a requirement, even if you are just testing with static files, because the file: protocol does not allow AJAX requests.
A simple example using infinite scrolling
Note: You can download this simple example from its GitHub repository.
Infinite scrolling is a common technique that sites like Twitter use for their feeds. When you reach the end of the list, infinite scrolling will load more items for you to continue reading.
This feature naturally requires JavaScript to check the page scroll position and load new items without refreshing the page. But htmx can do all this for you.
The following markup represents a list of posts, using an image for each post:
Imagine you have these items in one file, feed1.html, with more items in feed2.html, etc. Each page would then display a subset of items that you could scroll through:
You can now use the "next page" link to move from page to page, but you can also do infinite scrolling just as easily. Just change the last entry to:
Adding these 4 properties to the last list item will perform infinite scrolling. Here is the meaning of each attribute:
Properties | Value | Meaning |
---|---|---|
hx-trigger | revealed. revealed | When this element first appears on screen. |
hx-get | feed2.html | . send GET request to feed2.html, . |
hx-select | li | . select li elements from response, . |
hx-swap | afterend | .and add them after this element. |
As you scroll to the bottom of the list, notice how the page automatically loads new content. You can confirm this by observing your scrollbars and checking your browser's developer tools:
Note that, with this simple static page setup, the feed2.html page includes the last item which has the hx-get attribute set to feed3.html, etc. Also note how htmx has added a listener for the scroll event.
Other potential uses for htmx
You can use htmx for many other common interactions, including:
- Pagination: Loads and replaces items when visitors click on pagination links.
- Form validation: On submit, replace the form with a validation message or a subset of the form.
- Progress bar: Periodically updates the value of the progress bar based on the ping response.
- Inline editing: Swaps an element for the text area containing that element's value.
Each of these examples can be built using standard JavaScript, htmx just makes the process much easier.
Benefits and limitations of htmx
The benefits of htmx
htmx can greatly simplify common interactions that can benefit many web applications and even many websites. It allows designers and others working with front-end websites to add functionality without having to learn JavaScript.
By abstracting common behavior, htmx ensures consistency across your projects and across them. Infinite scrolling will work the same way from page to page, regardless of who implemented it.
Because it emphasizes a declarative (what) approach rather than an imperative (how) approach, htmx functionality is often easier to understand and reason about.
Limitations of htmx
While htmx can help you forget about JavaScript in many contexts, it can't solve all your problems! You still need to write code to handle specific business logic and many lower-level functions.
Because htmx does a lot of the work for you, there is less ability to customize behavior. This is often a positive trade-off, but you will need to be prepared to give up control.
Html can help you avoid writing JavaScript but you should still be aware that JavaScript code is running in the background. You may want to use the hx-get attribute on every link, instead of the href attribute. But this creates a dependency on JavaScript; If the code fails to run for any reason, your users will be left with a link that does nothing. You should always practice progressive raises to prevent this.
You should read it
- Funny series in Photoshop battle of photo-making experts (Part 3)
- Coc Coc adds dictionary lookup feature when surfing the web
- How to register for Mai Linh taxi driver
- Laptop has 2 screens
- How to register Techcombank Smart OTP to get the authentication code
- How to fix Not enough physical memory on VMware
- Guide to group chat on Instagram
- 11 interesting photos that explain the questions about the world around us
- How to Get a Motorcycle Loan With Bad Credit
- Not for editing content in PowerPoint
- Best tools for designing and administering Mysql databases
- Configuration steps for multiple Users to remotely connect to Windows Server 2016 using Remote Desktop