Jump to content

User:Gary/namespace redirect.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gary (talk | contribs) at 18:32, 12 May 2010 (fix). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*
	NAMESPACE REDIRECT
	Description: Redirects pages that begin with certain prefixes to appropriate namespaces.
	Example: [[C:Test]] -> [[Category:Test]], [[F:Test]] -> [[File:Test]], [[T:Test]] -> [[Template:Test]]
*/

addOnloadHook(function() {
	if (wgPageName == 'Special:Search')
	{
		var searchTerm = document.getElementById('searchText').value;
		var colon = searchTerm.indexOf(':');
	
		if (colon != -1)
		{
			var firstPart = searchTerm.substring(0, colon);
			var secondPart = searchTerm.substring(colon + 1);
			var pageName = '';
			
			switch (firstPart.toUpperCase())
			{
				case 'C':
					pageName = 'Category:' + secondPart;
					break;
				case 'F':
					pageName = 'File:' + secondPart;
					break;
				case 'T':
					pageName = 'Template:' + secondPart;
					break;
				case 'U':
					pageName = 'User:' + secondPart;
					break;
			}
			
			if (pageName != '')
				window.location = wgServer + '/wiki/' + pageName;
		}
	}
});