function count_length(target,display) {
  var text = target.value
  text = text.replace(/\r|\n/g, '').replace(/<[^>]+>/g, ''); /* 改行、タグ */
  text = unescape(escape(text).replace(/(%u3000|%20|%09)+|(%u3000|%20|%09)+/g, '')); /* 全角、半角スペース */
  var num = text.length;

  display.innerHTML = num;
}

function count_on_key_press(target_id, display_id) {
  var target = document.getElementById(target_id);
  var display = document.getElementById(display_id);
  target.onkeydown = function(){ count_length(target,display) };
  target.onkeyup = function(){ count_length(target,display) };
}

function count_on_load() {
  var target = document.getElementById("text");
  var display = document.getElementById("text_length");
  window.onload = function(){ count_length(target,display); };
}

count_on_load();
count_on_key_press("text","text_length");

