Www.wikipedia.org template/js

From Meta, a Wikimedia project coordination wiki

Jump to: navigation, search

Important: Editing this page will not affect the Wikipedia portal. See Talk:Www.wikipedia.org template/temp for instructions on updating or modifying the portal.

[edit] Development version

This is the original source of the JavaScript embedded within the <head> tag at the www.wikipedia.org template:

function $(id) { return document.getElementById(id) };
 
function addLoadEvent(fn) {
	if (window.addEventListener) addEventListener("load", fn, false);
	else if (window.attachEvent) attachEvent("onload", fn);
}
 
addLoadEvent(function () { $("searchInput").focus(); });
 
function getLang() {
	var uiLang = navigator.language || navigator.userLanguage;
	var results = document.cookie.match("(?:^|\W)searchLang=([^;]+)");
	return results ? results[1].toLowerCase() : uiLang;
}
 
function selectLanguage() {
	var lang = getLang();
	if (!lang) return;
	var iso639 = lang.match(/^\w+/);
	if (!iso639) return;
	iso639 = (iso639[0] == "nb") ? "no" : iso639[0];
	var langs =
		"ar,ca,cs,da,de,en,es,eo,fr,id,it,hu,nl,ja,ko,no,pl,pt,ro,ru,sk,sr,fi,sv,tr,uk,vi,vo,zh,";
	if (langs.indexOf(iso639 + ",") >= 0) {
		$("language").value = iso639;
	}
}
addLoadEvent(selectLanguage);
 
//function convertChinese(lang) {
//	if ("zh-hans,zh-cn,zh-sg,zh-my,".indexOf(lang + ",") < 0) return;
//	$('zh_1').innerHTML = "自由的百科全书";
//	$('zh_2').innerHTML = "条目";
//}
 
// Assumes the Chinese Wikipedia domain to be "http://zh.wikipedia.org/".
function convertZhLink(link, locale) {
	var domain = "http://zh.wikipedia.org/";
	if (link.href.indexOf(domain) < 0) return;
	link.href = link.href.replace(domain + "wiki", domain + "zh-" + locale);
}
 
function convertZhLinks() {
	var lang = getLang();
	if (lang.indexOf("zh") != 0) return;
	var locale = lang.substring(3 /* "zh-".length */);
	if (locale == "mo") locale = "hk";
	else if (locale == "my") locale = "sg";
	if ("cn,tw,hk,sg,".indexOf(locale) >= 0) {
		convertZhLink($("zhwiki"), locale);
		convertZhLink($("zhothers"), locale);
	}
}
addLoadEvent(convertZhLinks);
 
function setLang(lang) {
	var uiLang = navigator.language || navigator.userLanguage;
	var date = new Date();
	if (uiLang.match(/^\w+/) == lang) date.setTime(date.getTime() - 1);
	else date.setFullYear(date.getFullYear() + 1);
	document.cookie = "searchLang=" + lang + ";expires=" + date.toUTCString() +
		";domain=" + location.host + ";";
}

[edit] Released version

For the final output, the source has been minified using /packer/ (with the "Shrink variables" option enabled) to reduce page size:

function $(a){return document.getElementById(a)};function addLoadEvent(a){if(window.addEventListener)addEventListener("load",a,false);else if(window.attachEvent)attachEvent("onload",a)}addLoadEvent(function(){$("searchInput").focus()});function getLang(){var a=navigator.language||navigator.userLanguage;var b=document.cookie.match("(?:^|\W)searchLang=([^;]+)");return b?b[1].toLowerCase():a}function selectLanguage(){var a=getLang();if(!a)return;var b=a.match(/^\w+/);if(!b)return;b=(b[0]=="nb")?"no":b[0];var c="ar,ca,cs,da,de,en,es,eo,fr,id,it,hu,nl,ja,ko,no,pl,pt,ro,ru,sk,sr,fi,sv,tr,uk,vi,vo,zh,";if(c.indexOf(b+",")>=0){$("language").value=b}}addLoadEvent(selectLanguage);function convertZhLink(a,b){var c="http://zh.wikipedia.org/";if(a.href.indexOf(c)<0)return;a.href=a.href.replace(c+"wiki",c+"zh-"+b)}function convertZhLinks(){var a=getLang();if(a.indexOf("zh")!=0)return;var b=a.substring(3);if(b=="mo")b="hk";else if(b=="my")b="sg";if("cn,tw,hk,sg,".indexOf(b)>=0){convertZhLink($("zhwiki"),b);convertZhLink($("zhothers"),b)}}addLoadEvent(convertZhLinks);function setLang(a){var b=navigator.language||navigator.userLanguage;var c=new Date();if(b.match(/^\w+/)==a)c.setTime(c.getTime()-1);else c.setFullYear(c.getFullYear()+1);document.cookie="searchLang="+a+";expires="+c.toUTCString()+";domain="+location.host+";"}

Packer seems to be the best tool for the particular code we use at the Wikipedia portal, according to this comparison.

[edit] Testing

Revision 1735862 is known to work in the following browsers:

  • Mozilla Firefox 3.5.5 (Gecko), Mac OS X 10.5
  • Mozilla Firefox 3.6b4 (Gecko), Mac OS X 10.5
  • Safari 4.0.4 (WebKit), Mac OS X 10.5
  • Google Chrome 4.0.249.4 (WebKit), Mac OS X 10.5
  • iCab 4.7 (WebKit), Mac OS X 10.5
  • Opera 10.01 (Presto), Mac OS X 10.5
  • Microsoft Internet Explorer 6.0 (Trident), CrossOver Mac

It doesn't work in the following browsers:

  • iCab 3.0.5, Mac OS X 10.4

Please add your setup to the list above.