Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -2648,7 +2648,7 @@ var htmx = (function() {
if (!nodeData.loaded) {
nodeData.loaded = true
triggerEvent(elt, 'htmx:trigger')
handler(elt)
handler(elt, makeEvent('load', { elt }))
}
}
if (delay > 0) {
Expand Down Expand Up @@ -3687,7 +3687,7 @@ var htmx = (function() {
* @param {string} prompt
* @returns {HtmxHeaderSpecification}
*/
function getHeaders(elt, target, prompt) {
function getHeaders(elt, target, prompt, event) {
/** @type HtmxHeaderSpecification */
const headers = {
'HX-Request': 'true',
Expand All @@ -3696,7 +3696,7 @@ var htmx = (function() {
'HX-Target': getAttributeValue(target, 'id'),
'HX-Current-URL': location.href
}
getValuesForElement(elt, 'hx-headers', false, headers)
getValuesForElement(elt, 'hx-headers', false, headers, event)
if (prompt !== undefined) {
headers['HX-Prompt'] = prompt
}
Expand Down Expand Up @@ -4420,7 +4420,7 @@ var htmx = (function() {
}
}

let headers = getHeaders(elt, target, promptResponse)
let headers = getHeaders(elt, target, promptResponse, event)

if (verb !== 'get' && !usesFormData(elt)) {
headers['Content-Type'] = 'application/x-www-form-urlencoded'
Expand Down
6 changes: 6 additions & 0 deletions www/content/attributes/hx-headers.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ If you wish for `hx-headers` to *evaluate* the values given, you can prefix the
<div hx-get="/example" hx-headers='js:{myVal: calculateValue()}'>Get Some HTML, Including a Dynamic Custom Header from Javascript in the Request</div>
```

When using evaluated code you can access the `event` object.

```html
<div hx-get="/example" hx-headers='js:{"hx-trigger-event": event.type}'>Get Some HTML, Including a custom header indicating which event triggered the request</div>
```

## Security Considerations

* By default, the value of `hx-headers` must be valid [JSON](https://developer.mozilla.org/en-US/docs/Glossary/JSON).
Expand Down