大网数据广告活动
网站运营与故障问答

Is it possible to disable indexing through JS?

· 52CE 我爱测

Is it possible to disable Google indexing of some web page if this is placed to head tag <meta name="googlebot" content="noindex" /> using only JS? Or is it all a hoax and only the finished page is taken into account when indexin…

问题原文

原帖链接 · BlackStar1991 · CC BY-SA 4.0

Is it possible to disable Google indexing of some web page if this is placed to head tag

<meta name="googlebot" content="noindex" />

using only JS? Or is it all a hoax and only the finished page is taken into account when indexing?

已采纳解答

原帖链接 · Stephen Ostermiller · CC BY-SA 4.0

Google supports inserting meta tags with JavaScript, including noindex. You can have JavaScript insert <meta name="googlebot" content="noindex" /> into the <head> of the page and Google will stop indexing the page once it renders it.

Google recommends inserting noindex with JavaScript in its documentation: Understand JavaScript SEO Basics | Google Search Central  |  Google Developers

Avoid soft 404 errors in single-page apps

In client-side rendered single-page apps, routing is often implemented as client-side routing. In this case, using meaningful HTTP status codes can be impossible or impractical. To avoid soft 404 errors when using client-side rendering and routing, use one of the following strategies:

  • Use a JavaScript redirect to a URL for which the server responds with a 404 HTTP status code (for example /not-found).
  • Add a <meta name="robots" content="noindex"> to error pages using JavaScript.

...

Here is sample code for the noindex tag approach:

fetch(`/api/products/${productId}`)
.then(response => response.json())
.then(product => {
  if(product.exists) {
    showProductDetails(product); // shows the product information on the page
  } else {
    // this product does not exist, so this is an error page.
    // Note: This example assumes there is no other meta robots tag present in the HTML.
    const metaRobots = document.createElement('meta');
    metaRobots.name = 'robots';
    metaRobots.content = 'noindex';
    document.head.appendChild(metaRobots);
  }
})

The usual caveats about JavaScript rendering and SEO apply: It often takes Google extra time (weeks or even a couple months) to decide that it needs to render the page, render it, and react to the results. In other words inserting noindex with JavaScript is not likely to be as fast as inserting it into the HTML code on the back end.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2022-01-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。