在中使用jQuery添加标签是否可以?

huangapple go评论64阅读模式
英文:

Is it ok to add meta tag in head using jQuery?

问题

我想在HTML页面中有条件地添加元标签,我不确定是否可以使用jQuery的ready事件来这样做。以下是示例。

var myHost = window.location.host;
if (myHost.startsWith("techstag")) {
   $("head").append('<meta name="robots" content="noindex,nofollow">');
}
英文:

I want to add meta tag conditionally in an HTML page, i am not sure if it is ok to do so or not using jQeruy ready even. The example is given bellow.

var myHost = window.location.host;
if(myHost.startsWith(&quot;techstag&quot;)) {
   $(&quot;head&quot;).append(&#39;&lt;meta name=&quot;robots&quot; content=&quot;noindex,nofollow&quot;&gt;&#39;);
}

答案1

得分: 3

简而言之,它可能会起作用,也可能不会。

从检查页面时,您会看到页面中的元标记,从这个意义上说,它会起作用。

但是,如果您打算出于SEO目的而这样做:

  1. 大部分情况下,它不会起作用,因为大多数网络爬虫在扫描网页时不会执行JavaScript。
  2. 对于执行JS的爬虫(例如Google的爬虫),它会起作用。
英文:

In short, it may or may not work.

It would work in the sense you'll see the meta tag in page when you inspect it.

But, if you are intending to do this for SEO purposes

  1. For the most part, it won't work, as most web crawlers do not execute javascript while scanning the web pages.
  2. It will work for crawlers that do execute JS (e.g. Google's)

huangapple
  • 本文由 发表于 2020年1月6日 19:36:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611435.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定