1

We are currently working on a website using the Sprig Plugin to show and reactively filter event overviews.

One of the options is to browse through monthly views, showing you all information for one month at a time. In this context we also update the URL the s-push-url attribute and we also want to update <title> and maybe some meta tags.

Our way to do it is by listening to the htmx:afterOnLoad event and fetching title information from a certain header:

<script>
    document.body.addEventListener('htmx:afterOnLoad', function(event) {
        // Check if the response includes a custom header or data with the new title
        var newTitle = event.detail.xhr.getResponseHeader('X-Page-Title');
        if (newTitle) {
            document.title = newTitle;
        }
    });
</script>

This works but I do not really like it. Are there any other solutions for this requirement?

Thanks!

1 Answer 1

2

The simplest way to update the title is to include a title element anywhere in the response.

<title>New title</title>

I’m not sure if the same is true for meta tags. You can ask I in the htmx discord server, since swap functionality is handled by htmx.

1
  • Works great! Thanks! I thought just tags with an id would get replaced. Commented Oct 15 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.