User:Mike Dillon/Scripts/searchNewWindow.js

From Wikipedia, the free encyclopedia
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.
// Requires: [[User:Mike Dillon/Scripts/i18n.js]], [[User:Mike Dillon/Scripts/easydom.js]]

// <pre><nowiki>

// searchNewWindowDefault: Controls default state of checkbox on page load; "false" if unspecified
var searchNewWindowDefault;
if (searchNewWindowDefault == null) {
    searchNewWindowDefault = false;
}

// Messages
wfAddMsg("en", "searchNewWindowLabel", "New window?");
wfAddMsg("es", "searchNewWindowLabel", "¿Ventana nueva?");

addOnloadHook(function () {
    // Find the search form
    var searchform = document.getElementById("searchform");
    if (!searchform) return;

    // Get the first div inside (holds the input elements)
    var searchdiv = searchform.getElementsByTagName("div")[0];
    if (!searchdiv) return;

    with (easydom) {
        // Build the checkbox and onchange handler
        var newWindowCheckbox = input({
            "id": "searchNewWindow",
            "type": "checkbox",
            "onchange": function () {
                if (this.checked) {
                    searchform.setAttribute("target", "_blank");
                } else {
                    searchform.setAttribute("target", "_top");
                }
            }
        });

        // Add the checkbox and label to the div
        searchdiv.appendChild(div(
            { "class": "searchNewWindow" },
            newWindowCheckbox, " ",
            label({ "for": "searchNewWindow" }, wfMsg("searchNewWindowLabel"))));
    }

    // If searchNewWindowDefault is true, click the checkbox
    if (searchNewWindowDefault) {
        newWindowCheckbox.click();
    }
});

// </nowiki></pre>