4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ちょっとしたJavaScriptライブラリ作ってみた

Last updated at Posted at 2012-03-16

//copyright:@Meaue 2011.4.28

function executeTimeLink(){
  var timeLinkList = getTimeLinkList();
  for(var i=0; i<timeLinkList.length; i++){
    //timetag is not necessarily class...
    var timetag = timeLinkList(i).id;
    if( timetag != null){
      var content=timetag.split("timelink");
      //supposing... id="timelink_start:2002.12.31_end:2004.12.12"
      if(content[0]=="" && content[1]!=null){
        var limits = content[1].split("_");
        for(var m=0; m<limits.length; m++){
          if(limits[m].match(/./)){
            var methodAndTime = limits[m].split(":");
            var now = new Date();
            //Further extension is possible.
            alert(methodAndTime[0]);
            if(methodAndTime[0]=="start" && isValidTimeLink(methodAndTime[1])){
              //dissapear if date is before start
              if(now-toDate(methodAndTime[1])< 0){
                dissapear(timeLinkList(i));
              }
            }
            if(methodAndTime[0]=="end" && isValidTimeLink(methodAndTime[1])){
              //disappear if date is after end
              if(now-toDate(methodAndTime[1])>0){
                disappear(timeLinkList(i));
              }
            }
          }
        }
      }
    }
  }
}

function getTimeLinkList(){
  var elemList = document.getElementsByTagName("div");
  return elemList;
}

function disappear(timeLink){
  //make the tag empty
  document.getElementById(timeLink.id).innerHTML = "";
}

function toDate(time){
  var contents = time.split(".");
  return new Date(contents[0], contents[1]+1, contents[2]);
}

function isValidTimeLink(str){
  //leap year should be concerned...
  if(str.match(/./)){
    var contents = str.split(".");
    if(contents.length == 3){
      return true;
    }
  }
  return false;
}

window.onload=executeTimeLink;
4
4
2

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?