繁体   English   中英

动态为某些网址创建noindex元标记

[英]Dynamically creating noindex meta tags for certain URLs

因此,我试图制作一个switch语句,以在页面是某个URL时将以下代码嵌入文档头中:

<meta name="robots" content="noindex">

目前,我看起来像这样:

switch(document.URL){
  case "url/i/want/to/noindex":
    var m = document.createElement('meta'); 
    m.name = 'robots'; 
    m.content = 'noindex'; 
    document.head.appendChild(m);
    break;
  ...
}

但是,它似乎没有按预期工作。 我要解决这个错误吗?

大多数搜索引擎将忽略这一点,因为它们正在抓取HTML而不是后处理的DOM信息。 也就是说,您正在寻找的更像是这样:

if (window.location.href.indexOf("url/i/want/to/noindex") >= 0) {
    var m = document.createElement('meta'); 
    m.name = 'robots'; 
    m.content = 'noindex'; 
    document.head.appendChild(m);
}

document.URL和window.location.href将返回URL路径,包括域名,协议,端口等。因此,您只想搜索URL路径。 您可以想出许多巧妙的方法,包括使用正则表达式匹配模式或过滤掉URL路径之前的内容。 您也可以改用window.location.pathname,但是我不确定哪些浏览器支持它。

简而言之,就是您的switch语句中的测试条件不匹配。 例如,此页面上的document.URL是:

http://stackoverflow.com/questions/37977060/dynamically-creating-noindex-meta-tags-for-certain-urls/37977662#37977662

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM