最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

theme development - IS there any reason not to include javascript in my own post's embeds?

programmeradmin0浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

Does 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>
发布评论

评论列表(0)

  1. 暂无评论