根据浏览器语言指定两个元描述

问题描述 投票:0回答:1

我想基于浏览器语言指定两个元描述。一种是英语。另一个是日本人。我在下面尝试了两种方法:

1:

<head>
   <meta
      name="description"
      lang="en-us"
      content="Explore new opportunity."
    />
    <meta
      name="description"
      lang="ja-jp"
      content="ダンサー。"
    />
</head>

2:

</head>
 <script type="text/javascript">
      const isJapanese = window.navigator.language.toLowerCase() === 'ja-jp' || window.navigator.language.toLowerCase() === 'ja' ;
      var description= isJapanese ? "ダンサー。" : "Explore new opportunity.";

      console.log(description);
<meta
      name="description"
      lang="en-us"
      content=description
    />
    <meta
      name="description"
      lang="ja-jp"
      content=description
    />

</script>
</head>

但是没有运气。即使浏览器语言是日语,它也始终显示英语元描述。有帮助吗?

html meta-tags google-search-console
1个回答
4
投票

对于您的变体#2,您需要一个衬板来更新元内容描述,如下所示:

document.head.querySelector('meta[name="description"]').content = description; 
© www.soinside.com 2019 - 2024. All rights reserved.