So, how does this work:
Well for a start, don't worry, this isn't one of those dodgy uses of methods like onBlur and focus to trick you in to thinking a window is modal. This method uses an actual method of Internet Explorer's window object called showModalDialog.
To use this method, the first thing we need is to add a couple of functions to the parent form. Here's the first:
function myColourDialog(){
var myColour;
}
This is simply there to act as a container object for the variables that you want to pass back and forth. You can change the name of this function and add as many variables as you need. Let's see what the next function looks like:
function openColourPickerDialog(wnd, field, dlgURL)
{
myColourDialog.myColour="";
if(wnd.showModalDialog(
dlgURL,
myColourDialog,
"dialogHeight:150px;dialogWidth:300px;")
== true )
{
field.value = myColourDialog.myColour;
}
else
{
return;
}
}
Here we initialise the value of the "myColour" variable and then open the Pop-Up. Notice that we passed the myColourDialog object in as one the arguments. This is how the new window knows what values we are working with.
The above function works by waiting for a returnValue from the Pop-Up. This will be true or false depending on whether the user cancels the pop-up or chooses a colour. If they choose a colour and then the return value is true the field on the parent has its value set to that of the myColour variable.
To call the pop-up we use a call like the one below in somwhere like the onClick event of a button:
openColourPickerDialog(window, document.forms[0].TextOutput, '9F0D8552077A408686256999005AC70D/$file/new_window.htm')
As part of this call we have passed the parent window's object. We also pass the field to which we want the chosen value returned and the URL of the page to use to display the choices.
All you need now is to add a function on to the pop-up form itself:
function sendAndClose( selObj ){
if ( selObj.selectedIndex != 0 ){
window.dialogArguments.myColour =
selObj.options[selObj.selectedIndex].text;
window.returnValue=true;
window.close();
}
}
This is then called from a button on the form in the pop-up, in its onclick event, like so:
sendAndClose(this.form.Colours);
"Colours" being the name of the drop-down field in our example.
So, there you have it. This is the minimum you need to exchange data with modal pop-ups. You can take it as far as you need by editing the functions I've listed above to your needs. This can really be quite powerful once you understand the basics...
One small thing:
As great as this method may appear it does have one drawback. You can only use static pages in the pop-up. So, for instance, you couldn't display a view with a Next and Previous link, as this would require the page to be submitted back to the server. IE doesn't like that.
Other reading:
You can learn more about this method of the window object at the Microsoft site: About the showModalDialog method
Copyright © 2000 - 2024 Jake Howlett of Rockall Design ltd. This article was printed from codestore.net