How do you set html / body tag attributes in Meteor.js?
Asked Answered
C

3

15

I need to set attributes on the html tag or alternatively the body tag of the document in a Meteor.js application.

Specifically I want to have <html dir="rtl"> or <body dir="rtl">..

Trying the latter, I receive the console message:

While building the application:
client/views/layout/layout.html:7: Attributes on <body> not supported

=> Your application has errors. Waiting for file change.

So how do you do this?

Centner answered 14/1, 2014 at 8:26 Comment(0)
P
19

You have to inject them on start-up in your client-side Javascript:

Meteor.startup(function() {
   $('html').attr('dir', 'rtl');
});

UPDATE

Note that you can now set attributes in-line for body tags, and they'll be concatenated by Meteor in the same way as the contents of the body tag:

<body data-atttribute="foobar"></body>

You can have multiple different body tags, and they'll get combined, so the above will just add a single attribute to your existing body, rather than replacing it.

To the best of my knowledge, HTML tag attributes still need to be set via Javascript.

Piperine answered 14/1, 2014 at 8:54 Comment(2)
@richsliv is there non js solution?Violante
Meteor is a javascript framework, but you could use coffeeScript.Helbonnah
R
9

Attributes for HTML tag can be set on startup by using WebApp.addHtmlAttributeHook function. Here is an example:

Meteor.startup(function() {
    WebApp.addHtmlAttributeHook(function() {
        return {
            "dir": "rtl"
        }
    })
});

Make sure you call this on server, not client.

Rowland answered 22/3, 2016 at 19:15 Comment(1)
It can also be done with helmet: github.com/gatsbyjs/gatsby/issues/1790#issuecomment-334099975Seminary
P
1

In case someone else stumbles upon this to realise the above solutions don't work anymore, here's what worked for me with Meteor 2.13:

Meteor.startup(() => {
  document.documentElement.setAttribute('dir', 'rtl');
});

Found it here: https://github.com/meteor/meteor-feature-requests/issues/322

Pictorial answered 29/8, 2023 at 20:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.