LoginSignup
1
1

More than 5 years have passed since last update.

【JavaScript】headタグ内のmeta情報(keywordsとdescription)を取得できるスクリプト

Last updated at Posted at 2018-03-16

そのページのkeywordsとdescriptionを取得できるスクリプト

このページのdescriptionって何設定してんのかな~とか思ったりしたんで作ってみました。
使いどころ?しらんです

ソースコード

var headChildren = document.head.children;
for (var i = 0; i < headChildren.length; i++) {

  if (typeof headChildren[i].getAttribute('http-equiv') !== "undefined") {
    var propertyHttpEquiv = headChildren[i].getAttribute('http-equiv');
  }

  if (typeof headChildren[i].getAttribute('name') !== "undefined") {
    var propertyName = headChildren[i].getAttribute('name');
  }

  if (propertyName === 'description' || propertyHttpEquiv === 'description') {
    window.prompt('description:',headChildren[i].content);
  }

  if (propertyName === 'keywords' || propertyHttpEquiv === 'keywords') {
    window.prompt('keywords:',headChildren[i].content);
  }

}

これをchromeだったらF12押してデベロッパーツール開いてコンソール画面に張り付けエンター押せばOKです。
設定していないサイトはとれないです。あとサイトによって調整いるかも・・・

おわり

  • jQuery使わないでつくってみました
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1