April 20, 2009

XUL: Passing arguments and displaying a dialog

"The following code demonstrates how to pass custom arguments to a dialog, process those arguments in the dialog, and return user-modified arguments to the caller. The code to open a dialog named mydialog.xul and pass it arguments:

var params = {inn:{name:"foo", description:"bar", enabled:true}, out:null};
window.openDialog("chrome://myext/content/mydialog.xul", "",
"chrome, dialog, modal, resizable=yes", params).focus();
if (params.out) {
// User clicked ok. Process changed arguments; e.g. write them to disk or whatever
}
else {
// User clicked cancel. Typically, nothing is done here.
}

mydialog.xul:














mydialog.js:

// Called once when the dialog displays
function onLoad() {
// Use the arguments passed to us by the caller
document.getElementById("name").value = window.arguments[0].inn.name;
document.getElementById("description").value = window.arguments[0].inn.description;
document.getElementById("enabled").checked = window.arguments[0].inn.enabled;
}

// Called once if and only if the user clicks OK
function onOK() {
// Return the changed arguments.
// Notice if user clicks cancel, window.arguments[0].out remains null
// because this function is never called
window.arguments[0].out = {name:document.getElementById("name").value,
description:document.getElementById("description").value,
enabled:document.getElementById("enabled").checked};
return true;
}
" developer.mozilla.org

No comments:

Post a Comment