I'm looking at customising the embeds of posts for a theme I am creating. Looking at header-embed.php
in Theme_Compat
I see that the head has the following class="no-js"
.
Does this mean that including JavaScript would be a bad idea? (If so why?)
I'm looking at customising the embeds of posts for a theme I am creating. Looking at header-embed.php
in Theme_Compat
I see that the head has the following class="no-js"
.
Does this mean that including JavaScript would be a bad idea? (If so why?)
Share Improve this question asked Sep 15, 2019 at 15:58 Matthew Brown aka Lord MattMatthew Brown aka Lord Matt 1,0683 gold badges13 silver badges34 bronze badges1 Answer
Reset to default 1Does this mean that including JavaScript would be a bad idea?
No, it doesn't.
That class is used for styling purposes so that you could apply styles specific to when JavaScript is not available (either not supported or is disabled), and when it's available (supported and enabled). And in most cases/implementations, when JS is enabled, the no-js
class will be replaced with js
. (I mean, you can use another name, if you want to...)
/* Sample CSS. Allows you to have something like:
<p class="hide-if-js">Hidden if JS is enabled</p>
<p class="hide-if-no-js">Hidden if JS is not enabled</p>
*/
.js .hide-if-js,
.no-js .hide-if-no-js {
display: none;
}
And for the WordPress post embeds, here's the code which replaces the class name — but only if certain conditions are met (e.g. window.addEventListener
is supported by the browser):
document.documentElement.className = document.documentElement.className.replace( /\bno-js\b/, '' ) + ' js';
In fact, WordPress admin pages also use the same trick and you can see the relevant code in wp-admin/admin-header.php
:
<body class="wp-admin wp-core-ui no-js <?php echo $admin_body_classes; ?>">
<script type="text/javascript">
document.body.className = document.body.className.replace('no-js','js');
</script>