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

I'm looking at customising the embeds of posts for a theme I am creating. Looking at header-embed.php in Theme_Comp

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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745155924a4614119.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信