MediaWiki talk:Charset.js

From Wikipedia, the free encyclopedia

This is the improved code User:Locke Cole and I created at User:Locke Cole/edittoolstest.html. It removes the need to put languages in the javascript code and will save the language in a cookie so someone who always uses the same one doesn't have to reselect it. The current page can be replaced with the below version in its entirety. I have tested it in my monobook.js and it seems to be working fine. Copy what's between "<!----><pre>" and "<!----></pre>" in the wikisource.

/********************/
/* Cookie functions */
/********************/

/* returns a cookie value for the specified cookie name, or blank ('') */
function fnGetCookie(name) {
  var cookiestring = '' + document.cookie;
  var idx1 = cookiestring.indexOf(name);
  if ((idx1 == -1) || (name == '')) 
    return ''; 
  var idx2 = cookiestring.indexOf(';', idx1);
  if (idx2 == -1) 
    idx2 = cookiestring.length; 

  return unescape(cookiestring.substring(idx1 + name.length + 1, idx2));
}

/* sets a cookie with a one year expiration */
function fnSetCookie(name, value) {
  var y = 365 * 24 * 60 * 60 * 1000;
  fnSetCookie(name, value, y);
}

/* sets a cookie with a with a duration given in milliseconds */
function fnSetCookie(name, value, duration) {
  var d = new Date();
  d.setTime(d.getTime() + duration);
  var c = name + '=' + escape(value) + ';expires=' + d.toUTCString();
  document.cookie = c;
}

/******************************/
/* Charset specific functions */
/******************************/

/* select subsection of special characters */
function fnCharsetChooseSubset(s) {
  var l = document.getElementById('specialchars').getElementsByTagName('p');

  for (var i = 0; i < l.length ; i++) {
    l[i].style.display = i == s ? 'inline' : 'none';
    l[i].style.visibility = i == s ? 'visible' : 'hidden';
  }
}
/* create charset dropdown box */
function fnCharsetLoad() {
  var l = document.getElementById('specialchars');
  if (l) {
    l.innerHTML = '<select id="charsetBox" style="display: inline" onkeyup="fnCharsetChooseSubset(selectedIndex)" onchange="fnCharsetChooseSubset(selectedIndex)">' + l.innerHTML;
  }

  var b = document.getElementById('charsetBox');
  var e = document.getElementById('editform');
  if (l)
    l = l.getElementsByTagName('p');
  var c = fnGetCookie('charsetDefault');

  if ((b) && (l)) {
    b.innerHTML = '';
    if (!((c) && (c != '')))
      c = l[0].id;
    for (var i = 0; i < l.length; i++) {
      var o = document.createElement('option');
      if (c == l[i].id)
        o.selected = true;
      
      var n = unescape(decodeURI(l[i].id.replace(/\.([a-fA-F0-9][a-fA-F0-9])/g, '%$1')));
      n = n.replace(/_/g, ' ');
      o.appendChild(document.createTextNode(n));
      b.appendChild(o);
    }

    if(window.addEventListener){
      e.addEventListener('submit',fnCharsetSaveSelection,false);
    } else if(window.attachEvent){
      e.attachEvent('onsubmit',fnCharsetSaveSelection);
    }
    fnCharsetChooseSubset(b.selectedIndex);
  }
}

/* Determines which option is selected and saves a cookie containing the relevant paragraph's id */
function fnCharsetSaveSelection() {
  var b = document.getElementById('charsetBox');
  var l = document.getElementById('specialchars').getElementsByTagName('p');
  var s = b.selectedIndex;

  if (s <= l.length) {
    fnSetCookie('charsetDefault', l[s].id);
  }
}

addLoadEvent(fnCharsetLoad);

<!---->


  • I tried doing this, and when I refreshed, the entire content of the more characters region was blank. There was only a blank dropdown menu. — 0918BRIAN • 2006-01-17 13:29
    • I was able to get a version of the above code working, but the cookie doesn't seem to work in IE anyway, so let's leave this as is for now. Superm401 | Talk