MediaWiki:DefaultSearch.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
//
// Change default from Go to Search; loaded through [[MediaWiki:Monobook.js]]
// Hence loaded from bottom of the page, no need to wait for DOM ready.
//

// jshint valid
/*jshint curly:false */


// wait for user's js
mw.loader.using(['user'], function() {
	// Checking user preference
	if (window.GoButton) return;

	// #searchform is for Monomook, #searchform-sidebar and -footer are for CologneBlue.
	// ([[CologneBlue.js]] does not load this file as of 2020-09-27,
	// but the old code supported it, so who knows, it might be important.)
	var $searchBtn = $('#searchform .searchButton[name=fulltext], #searchform-sidebar .searchButton[name=fulltext], #searchform-footer .searchButton[name=fulltext]'),
		$goBtn = $('#searchform .searchButton[name=go], #searchform-sidebar .searchButton[name=go], #searchform-footer .searchButton[name=go]'),
		$textfield = $('#searchform input[name=search], #searchform-sidebar input[name=search], #searchform-footer input[name=search]');

	$textfield.keydown(function(e) {
		if (e.which === 13) { // Enter
			$searchBtn.click();
			return false;
		}
	});

	// Indicate default visually. Changing the style of a button works also in IE. (However,
	// Safari seems to ignore this).
	$goBtn.css('fontWeight', 'normal');
	$searchBtn.css('fontWeight', 'bold');
});