Difference between revisions of "MediaWiki:Common.js"

From JReviews Documentation
Jump to: navigation, search
(Created page with 'Any JavaScript here will be loaded for all users on every page load.: // //change default from Go to Search // // Global: addHandler, addOnloadHook from wikibits.js add…')
(No difference)

Revision as of 20:03, 2 November 2009

/* Any JavaScript here will be loaded for all users on every page load. */
 
//
//change default from Go to Search
//
 
// Global: addHandler, addOnloadHook from wikibits.js
 
addOnloadHook(
  function ()
  {
    function changeButtons (formId, suffix)
    {
      var searchForm = document.getElementById (formId);
      if (!searchForm) return;
      // Classic skin has no IDs on the buttons, just names! And CologneBlue and Classic have
      // two search forms.
 
      function getFormElement (form, id, name)
      {
        return form.elements[id] || form.elements[name] || null;
      }
 
      var goBtn     = getFormElement (searchForm, 'searchGoButton' + suffix, 'go');
      var searchBtn = getFormElement (searchForm, 'mw-searchButton' + suffix, 'fulltext');
      if (!goBtn || !searchBtn) return;
      try {
        // Change the "Go" submit-button into a normal button that submits through JS
        // That leaves the form with a single submit-button (the "Search" button), which thus
        // becomes the default submit action. This works in Firefox, Opera, Safari.
        goBtn.type = "button";
        goBtn.onclick = new Function( "this.form.submit()" );
      } catch (ex) {
        // You cannot change a form element's type in IE as it uses immutable Windows controls.
        // See e.g. http://www.developersdex.com/asp/message.asp?p=2978&r=6420654
        // But on IE, we can use a keydown handler that intercepts the enter key.
        var textfield = getFormElement (searchForm, 'searchInput' + suffix, 'search');
        if (!textfield) return;
 
        // Luckily, the keydown handler from mwsuggest.js doesn't do anything with enter.
        addHandler (textfield, 'keydown',
          function (evt) {
            var e = evt || window.event;
            var key = (typeof (e.keyCode) != 'undefined' ? e.keyCode : e.which);
            if (key == 13) { // Enter
              searchBtn.click (); // That's what we really want to do
              return false;
            }
          }
        );
      } // end try...catch
 
      // Indicate default visually. Changing the style of a button works also in IE. (However,
      // Safari seems to ignore this).
      goBtn.style.fontWeight = "normal";
      searchBtn.style.fontWeight = "bold";
 
    } // end changeButtons
 
    changeButtons ('searchform', "");
    changeButtons ('searchform2', "2");
  }
);