Skip to content

Commit

Permalink
Better explanation of Ajax alternatives.
Browse files Browse the repository at this point in the history
  • Loading branch information
gnat authored May 14, 2024
1 parent 7560166 commit 55386c9
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,15 @@ Append / Prepend elements.
* ▶️ `me().insertBefore(element, other_element.firstChild)`
* ▶️ `me().insertAdjacentHTML("beforebegin", new_element)`

Ajax (alternatives to jquery `ajax()`)
* First, check out [htmx](https://htmx.org/) ..and if you need more control:
* Using [fetch()](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) ▶️
AJAX (replacing jquery `ajax()`)
* Use [htmx](https://htmx.org/) or [htmz](https://leanrada.com/htmz/) or [fetch()](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) or [XMLHttpRequest()](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) directly.
* ▶️ `fetch()`
```js
me().on("click", async event => {
let e = me(event)
// Example 1: Hit an endpoint.
// EXAMPLE 1: Hit an endpoint.
if((await fetch("/webhook")).ok) console.log("Did the thing.")
// Example 2: Get content and replace me()
// EXAMPLE 2: Get content and replace me()
try {
let response = await fetch('/endpoint')
if (response.ok) e.innerHTML = await response.text()
Expand All @@ -336,15 +336,15 @@ me().on("click", async event => {
catch (error) { console.warn(`fetch(): ${error}`) }
})
```
* Using [XMLHttpRequest()](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) ▶️
* ▶️ `XMLHttpRequest()`
```js
me().on("click", async event => {
let e = me(event)
// Example 1: Hit an endpoint.
// EXAMPLE 1: Hit an endpoint.
var xhr = new XMLHttpRequest()
xhr.open("GET", "/webhook")
xhr.send()
// Example 2: Get content and replace me()
// EXAMPLE 2: Get content and replace me()
var xhr = new XMLHttpRequest()
xhr.open("GET", "/endpoint")
xhr.onreadystatechange = () => {
Expand Down

0 comments on commit 55386c9

Please sign in to comment.