LoginSignup
3
3

More than 5 years have passed since last update.

ブラウザ側で日時指定で、リダイレクト先を切り替えるスクリプト

Last updated at Posted at 2013-10-22

また、某所より要望があったので作りました。
SEO的にどうなのかは知りません。

ソースと使い方

ソースです。jQueryは別途用意してください。

$(document).ready(function() {
  $('a.location_timer').each(function(index, target) {
    var startDate = $(this).attr('data-start-date');
    var endDate = $(this).attr('data-end-date');
    var nowDate = new Date();

    startDate = startDate ? new Date(startDate) : nowDate;
    endDate = endDate ? new Date(endDate) : null;

    if (startDate <= nowDate && (!endDate || nowDate <= endDate)) {
      location.href = $(this).attr('href');
    }
  });
});

htmlでは下記のようにaタグに対して、location_timerクラスを指定して、このエントリで紹介しているものと同じようにdata-start-date属性とdata-end-date属性を追加すると

<a class="location_timer" href="http://qiita.com" data-start-date="2013/10/22 16:00" data-end-date="2013/10/30 23:59"></a>

href属性で指定されたURLにリダイレクトされます。

長所と短所

3
3
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
3
3