Jump to content

User:Writ Keeper/Scripts/SearchNamespace.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Writ Keeper (talk | contribs) at 20:11, 29 February 2012 (i hate bugs. and javascript.). 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.
// Allows the user to select a namespace to search in from the corner search bar

prefixString = 
"<option label='(article)'></option>\n <option>Talk:</option>\n <option label='WP'>Wikipedia:</option>\n <option labl='WP talk:'>Wikipedia talk:</option>\n <option>User:</option>\n <option>User talk:</option>\n<option>Template:</option>\n <option>Template talk:</option>\n <option>Help:</option>\n <option>Special:</option>";

addOnloadHook(addNSSearch);
function addNSSearch() 
{
    //retrieve existing elements that we're going to use
    searchBar = document.getElementById("searchInput");
    searchButton = document.getElementById("searchButton");
    searchForm = document.getElementById("searchform");
    searchDiv = document.getElementById("simpleSearch");

    //create new elements that we're going to insert
//    prefixDiv = document.createElement("div");
    prefixList = document.createElement("select");
    hiddenInput = document.createElement("input");

    //initialize new elements
//    prefixDiv.id = "prefixDiv";
    prefixList.id = "prefixList";
    prefixList.innerHTML = prefixString;
    hiddenInput.id = "hiddenInput";
    hiddenInput.name = "search";
    hiddenInput.type = "hidden";

    //insert new elements
    searchDiv.insertBefore(hiddenInput, searchBar);
    searchForm.insertBefore(prefixList, searchDiv);

    //modify old ones
    document.getElementById
    searchForm.addEventListener("onsubmit", createSearchTerm, false); //set onsubmit handler to concatenate selected prefix and entered search term
    searchBar.removeAttribute("name"); //since we're no longer using the search bar to submit the search terms directly
    searchDiv.style.display = "inline-block";
    prefixList.style.marginTop = "0.65em";
}

//onsubmit handler; concatenates prefixList and searchBar and inserts into hidden input prior to submission
function createSearchTerm()
{
    //retrieve used elements
    hiddenInput = document.getElementById("hiddenInput");
    prefixList = document.getElementById("prefixList");
    searchBar = document.getElementById("searchInput");
 
    //do the work
    hiddenInput.value = "" + prefixList.value + searchBar.value;
}