User:Writ Keeper/Scripts/SearchNamespace.js

// Allows the user to select a namespace to search in from the corner search bar

prefixString = " Article: \n Article talk: \n Wikipedia: \n Wikipedia talk: \n User: \n User talk: \n WikiProject: \n WikiProject talk: \n Category: \n Category talk: \n Template: \n Template talk: \n Book: \n Book talk: \n Help: \n Special: ";

$(document).ready(function {   //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");    if ( !searchDiv || !searchBar ) return; // mobile skin.

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

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

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

//modify old ones searchForm.onsubmit = createSearchTerm; searchForm.style.marginTop = "0px"; 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 = ".8em"; prefixList.tabIndex = searchBar.tabIndex; });

//onsubmit handler; concatenates prefixList and searchBar and inserts into hidden input prior to submission function createSearchTerm(e) {   //retrieve used elements hiddenInput = document.getElementById("hiddenInput"); prefixList = document.getElementById("prefixList"); searchBar = document.getElementById("searchInput"); //do the work (handling special cases) if(prefixList.value == "Article:") {       hiddenInput.value = "" + searchBar.value; }   else if(prefixList.value == "Article talk:") {       hiddenInput.value = "" + "Talk:" + searchBar.value; }   else if(prefixList.value == "WikiProject:") {       hiddenInput.value = "" + "Wikipedia:WikiProject " + searchBar.value; }   else if(prefixList.value == "WikiProject talk:") {       hiddenInput.value = "" + "Wikipedia talk:WikiProject " + searchBar.value; }   else {            hiddenInput.value = "" + prefixList.value + searchBar.value; } }