ColdFusion 8 added the CFWindow tag which creates a pop-up window in the browser, which is not really a pop-up window, but rather it is a div layer that looks like a window and can be hidden and shown via JavaScript. Today, I am going to demonstrate a couple ways you can expand your use of the CFWindow tag by incorporating some JavaScript that interacts with the underlying Ext.BasicDialog object.

This first example shows how to use the Ext.BasicDialog.isVisible() function to determine if the window is currently visible. Clicking the "Toggle Window" button calls a function that determines if the window is visible, then if it is visible it calls ColdFusion.Window.hide(), otherwise it calls ColdFusion.Window.show().

View working sample of the code below

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html>
<head>
   <title>CFWindow and Ext.BasicDialog.isVisible()</title>
   <script language="JavaScript">
      toggleWindow = function(){
         //Get window object          
         myWindow = ColdFusion.Window.getWindowObject('MyWindow');
         
         //use the isVisible function to determine if the window is already open          
         if (myWindow.isVisible()){
            ColdFusion.Window.hide('MyWindow');
         }
         else{
            ColdFusion.Window.show('MyWindow');
         }
      }
   </script>   
</head>
<body>
<form name="MyForm">
<input type="button" value="Toggle Window" onclick="toggleWindow();">
</form>
<cfwindow
name="MyWindow"
center="true"
closable="false"
draggable="true"
height="300"
initShow="false"
minHeight="100"
minWidth="200"
modal="false"
refreshOnShow = "false"
resizable="true"
title="My Test Window"
width="400">

window contents
</cfwindow>
</body>
</html>

This second example shows how you can use the CFWindow to display form validation errors. I think this is pretty cool because you can customize the look and feel of your for validation alerts instead of just using a plain old JavaScript alert box.

I created an init() function, which gets called via ajaxOnLoad(). The int() function uses Ext.BasicDialog.addButton to add an "OK" button to the window which, when clicked, will call a function that closes the CFWindow.

The throwError() function I created takes in an HTML string that contains the error messages and updates the body of the CFWindow, then opens it. You will notice in the CFWindow tag, that I set the "modal" attribute value to "true". This makes it so the user cannot interact with the main window while the CFWindow is visible.

I set up the form so when the "submit" button is clicked it calls a checkForm() function, which validates the data fields, and concatenates a list of error messages. If any errors are found the throwError() function is called.

click here for the working sample

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html>
<head>
   <title>CFWindow as Alert</title>
   <!--- import the button.js script to power the OK button --->
   <script src="/CFIDE/scripts/ajax/ext/package/button/button.js" type="text/javascript"></script>
   <script language="JavaScript">
      init = function(){
         //Get window object          
         myWindow = ColdFusion.Window.getWindowObject('ErrorWindow');
         
         //create an OK button          
         myWindow.addButton("OK", closeErrWin);
      }
      throwError = function(errMsg){
         //Get window object          
         myWindow = ColdFusion.Window.getWindowObject('ErrorWindow');
         
         //put the error message in the body          
         myWindow.body.update(errMsg);
         
         //Show the window          
         ColdFusion.Window.show('ErrorWindow');
      }
      closeErrWin = function(){
         //Close the error window          
         ColdFusion.Window.hide('ErrorWindow');
      }
      checkForm = function(){
         errMsg = "";
         //check required numeric field          
         if (MyForm.MyNumberField.value == "" || isNaN(MyForm.MyNumberField.value)){
            errMsg = (errMsg + "<li><font color=800000>Please enter a Numeric value in the Number field</font></li>");
         }
         //check required string field          
         if (MyForm.MyStringField.value == ""){
            errMsg = (errMsg + "<li><font color=800000>Please enter a string value in the String field</font></li>");
         }
         //if an error was found call call the TrowError function          
         if (errMsg != ""){
            throwError('<b>Please fix the following errors:</b><br><ul>' + errMsg + '</ul>')
         }
         else{
            //otherwise, submit the form             
            MyForm.submit();
         }
         
      }
   </script>   
</head>
<body>
<!--- simple form --->
<form name="MyForm">
<b>Number:</b> <input type="Text" name="MyNumberField"><br>
<b>String:</b> <input type="Text" name="MyStringField"><br>
<input type="button" value="Submit" onclick="checkForm();">
</form>
<!--- set up the error window. --->
<cfwindow
name="ErrorWindow"
center="true"
closable="true"
draggable="true"
height="200"
initShow="false"
minHeight="100"
minWidth="200"
modal="true"
refreshOnShow = "false"
resizable="true"
title="An Error Occured"
width="400">


</cfwindow>
<!--- call the init function when the cf ajax stuff is done loading --->
<cfset ajaxonload("init")>
</body>
</html>

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Dustin's Gravatar isVisible() is neat, but what if your Window might not even be created yet? The problem I have found is that there is no way to make that check in pure Javascript, before you make the call to getWindowObject. Problem with that is, getWindowObject will throw an exception if it doesn't exist. So you have to put it in a try Catch block to get around it - seems rather inelegant.

Also what would be nice is a way to get all the Windows that are open currently (in javascript). Do you know of a way to do that?
# Posted By Dustin | 12/19/07 3:51 PM
Scott Bennett's Gravatar @Dustin,

I just added a new post to answer your question as it was too long to put into a comment. Click on the "JavaScript to Identify Current CFWindows" link in the related blog entries above.
# Posted By Scott Bennett | 12/20/07 3:16 PM
Dustin's Gravatar Thank you!
# Posted By Dustin | 12/20/07 4:46 PM
Matt's Gravatar Hi - great stuff!

Is it possible to dynamically change the dimentions of the window through js?

M@)
# Posted By Matt | 1/23/08 6:03 AM
# Posted By Scott Bennett | 1/23/08 2:50 PM
Dustin's Gravatar thanks man. Really appreciate you sharing your wisdom with us.
# Posted By Dustin | 1/23/08 3:08 PM
Matt's Gravatar ACE! That's wicked.

Now that we're able to only require a single window and can dynamically change the content and window size, is there a way to reposition it in the center?

Is there a list of all of the methods and controls somewhere - I don't recall seeing this stuff on the adobe site ...

M@)

M@)
# Posted By Matt | 1/23/08 3:30 PM
Scott Bennett's Gravatar @Matt
You would use the center() function.


<script language="JavaScript">
centerWindow = function(windowName){
//Get window object
myWindow = ColdFusion.Window.getWindowObject(windowName);

//use the centerfunction to center the window.
myWindow.center();

}
</script>

a complete list of the public methods available for the Ext.BasicDialog object (the object used to create CFWindows) can be found on this page:

http://extjs.com/deploy/ext/docs/output/Ext.BasicD...
# Posted By Scott Bennett | 1/23/08 5:30 PM
Matt's Gravatar Thanks loads. So much to learn ....
# Posted By Matt | 1/28/08 5:11 PM
Matt's Gravatar Hi

How would I leverage some of the public events, such as: beforehide

I've tried:

myWindow = ColdFusion.Window.getWindowObject('MyWindow');
myWindow.beforehide = function(){
alert("hello");
}

I'm not too experienced with this. Thank you for helping.

M@)
# Posted By Matt | 1/28/08 6:46 PM
Tran's Gravatar Hi,
How would I use a cfwindow to enter in some info, submit or close the cfwindow and return the values back to the main page?
# Posted By Tran | 1/30/08 2:54 PM
Scott Bennett's Gravatar @Matt,

You would create an function that adds a listener to the event and use the ajaxonload tag to call it. I went ahead and wrote an entry with examples of how to do this:

http://www.coldfusionguy.com/ColdFusion/blog/index...
# Posted By Scott Bennett | 1/30/08 4:55 PM
Scott Bennett's Gravatar @Tran,

You could place a form within your cfwindow and then set up a "hide" event listener that checks the form field values and moves them to where you need them on the main page. If you want an example I can post one later today.
# Posted By Scott Bennett | 1/30/08 5:02 PM
Scott Bennett's Gravatar FYI, Dan Vega wrote an article where he shows how you can assign your own button icon and class when you call the Ext.BasicDialog.addButton function I used in my second example above.

http://www.danvega.org/blog/index.cfm/2008/3/7/Add...

I thought that was pretty cool and anyone reading this article should read Dan's too.
# Posted By Scott Bennett | 3/7/08 3:04 PM
Nathan Stanford's Gravatar I like the center feature but if you want to move the window to a little below center what function do you call to center then move it down 20 px?

Thanks,
Nathan
# Posted By Nathan Stanford | 4/22/08 1:02 PM
Scott Bennett's Gravatar @Nathan,

I made a quick example to answer your question and posted it here:

http://www.coldfusionguy.com/ColdFusion/blog/index...
# Posted By Scott Bennett | 4/22/08 4:36 PM
Ebenezer Anjorin's Gravatar cfwindows is quite nice, but i need it to behave in a particular way. i want to remove the frame and the (x) close button. i want to use cfwindow to just display the bigger size of an image on mouseover and close on mouse out.

anyone with an idea.

Thanks
# Posted By Ebenezer Anjorin | 5/29/08 12:38 AM
WeBoat's Gravatar These are great tips and have been very helpful with CFWINDOW.... BUT: I can't seem to find a way to change the source attribute.

What I do is create the window, and before it is first shown, I would like to change the "source" field so I can add URL parms to it. I know it's cached after it's loaded, but I was hoping it could be modified before it's first shown.
# Posted By WeBoat | 5/29/08 10:52 PM
Scott Bennett's Gravatar @Ebenezer,

I wrote a post on how to do what you want here:

http://www.coldfusionguy.com/ColdFusion/blog/index...

@WeBoat,

You can navigate the CFWindow to whatever URL you with with the ColdFusion.navigate() function.

http://livedocs.adobe.com/coldfusion/8/htmldocs/Ja...
# Posted By Scott Bennett | 5/30/08 7:35 PM
los's Gravatar I know this article is waaaay old but I just stumbled across it. I ran your second example and am curious as to how you managed to make the background of the modal cfwindow somewhat transparent. When I use this attribute, the background is a solid light grey and I can't see anything in the background.
# Posted By los | 11/18/10 6:22 PM
L Williams's Gravatar Scott, this is way cool -and I can use most of it for what I want, but I am still missing one piece. Can I pass form vars to a window for user validation before submission - meaning if I wanted a pop up window to show the user what they entered before I inserted into a database, could I do that? it could be a lot of text
# Posted By L Williams | 6/17/11 11:32 AM
https://www.bababorses.de/Louis-Vuitton-Damier-Ebene-Canvas-Clapton-PM-Bag-N44243-Magnolia-2361-it.html https://www.bababorses.de/Louis-Vuitton-LV-Trainer-Men-s-Sneakers-Top-Quality-15-5322-it.html https://www.bababorses.de/Celine-Small-Cabas-Bag-In-Black-Leather-it-2087 https://www.bababorses.de/Louis-Vuitton-Heel-10cm-Call-Back-Sandals-Nude-6162-it.html https://www.bababorses.de/LOUIS-VUITTON-BREA-MM-Monogram-Vernis-Leather-In-Magenta-4069-it.html https://www.bababorses.de/Louis-Vuitton-Ring-09-557-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-LV-Square-Espadrilles-Slipper-Sandals-Brown-6371-it.html https://www.bababorses.de/Prada-Golden-Saffiano-Calfskin-Leather-Top-Handle-Bag-it-2956 https://www.bababorses.de/Dior-Diorissimo-Small-Bag-Black-Nappa-Leather-Silvery-Hardware-8001-it-22 https://www.bababorses.de/Louis-Vuitton-Idylle-Blossom-Charms-Necklace-Q94360-406-it.html https://www.bababorses.de/Louis-Vuitton-Color-Blossom-BB-Star-Pendant-Necklace-Red-Gold-309-it.html https://www.bababorses.de/Bvlgari-Serpenti-Original-Leather-Framed-Pochette-Sky-Blue-82121-it-1938 https://www.bababorses.de/Louis-Vuitton-Horizon-55-Trolley-Travel-Luggage-Bag-Taiga-Leather-M30331-Red-6892-it.html https://www.bababorses.de/Fendi-By-The-Way-Small-Croc-Satchel-White-it-2731 https://www.bababorses.de/Louis-Vuitton-Monogram-Canvas-and-PVC-Nano-Bag-M61114-3176-it.html https://www.bababorses.de/Louis-Vuitton-Sarah-Multicartes-Wallet-M61273-Hot-Pink-7624-it.html https://www.bababorses.de/louis-vuitton-speedy-30--Damier-Azur-Canvas-n44367-2300-it.html https://www.bababorses.de/Hermes-Birkin-35cm-cattle-skin-vein-Handbags-blue-golden-it-907 https://www.bababorses.de/Saint-Laurent-Baby-Sac-De-Jour-Bag-In-Rose-Grained-Leather-it-3322 https://www.bababorses.de/Louis-Vuitton-Twist-MM-M53531-M53532-2775-it.html https://www.bababorses.de/Louis-Vuitton-Sunglasses-133-978-it.html https://www.bababorses.de/Louis-Vuitton-Neverfull-MM-M54185-Black-2705-it.html https://www.bababorses.de/Prada-Saffiano-East-West-Medium-Tote-Bag-Nero-it-3042 https://www.bababorses.de/Louis-Vuitton-Compact-Wallet-in-Monogram-Canvas-M63041-7399-it.html https://www.bababorses.de/Prada-Mens-Leather-Pouch-3312-Black-it-3099 https://www.bababorses.de/Louis-Vuitton-Women-s-Escale-Lock-It-Flat-Mule-1A7TOX-Pink-5965-it.html https://www.bababorses.de/Fendi-Baguette-Micro-Monster-Bag-Purple-Multi-it-533 https://www.bababorses.de/Louis-Vuitton-LV-Angel-Stud-Earrings-M64293-435-it.html https://www.bababorses.de/Louis-Vuitton-Damier-Ebene-Canvas-Zippy-Wallet-Evasion-M61360-7219-it.html https://www.bababorses.de/LOUIS-VUITTON-CATOGRAM-SQUARE-SCARF-MP2266-4818-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-Empreinte-Triangle-Shaped-Messenger-Bag-M54330-Black-3865-it.html https://www.bababorses.de/Balenciaga-Velo-Anthracite-store-it-1723 https://www.bababorses.de/Chloe-Marcie-Medium-Satchel-Bag-Cobalt-it-2283 https://www.bababorses.de/louis-vuitton-epi-leather-Soufflot-BB-bag-m55613-black-2580-it.html https://www.bababorses.de/Louis-Vuitton-Dauphine-MM-M55735-4512-it.html https://www.bababorses.de/Louis-Vuitton-Crafty-NeoNoe-MM-bag-black-M45497-2980-it.html https://www.bababorses.de/Louis-Vuitton-Men-Box-Bag-Shoulder-Body-Bag-M44157-Brown-3136-it.html https://www.bababorses.de/Prada-Saffiano-Double-Zip-Executive-Tote-Bag-Gray-it-3025 https://www.bababorses.de/Louis-Vuitton-Epi-Leather-NeoNoe-BB-Bucket-Bag-M53610-Indigo-2564-it.html https://www.bababorses.de/Saint-Laurent-Small-Monogram-Tassel-Satchel-In-Red-Crocodile-Leather-it-3158 https://www.bababorses.de/Louis-Vuitton-Iphone-Case-LV18-59-it.html https://www.bababorses.de/LOUIS-VUITTON--CLASSIC-MINI-PACKBACK-2872-it.html https://www.bababorses.de/Balenciaga-Velo-Anthracite-store-it-1723 https://www.bababorses.de/Louis-Vuitton-Monogram-Ebene-Canvas-Pegase-Legere-53-Business-Rolling-Luggage-6950-it.html https://www.bababorses.de/Louis-Vuitton-Crocodilien-Brillant-Capucines-Mini-Bag-N93429-Black-2231-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-Hoodie-Jacket-Black-1526-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-Coated-Canvas-Popincourt-PM-M43462--Raisin-3345-it.html https://www.bababorses.de/Louis-Vuitton-Lockme-Cabas-Tote-M55028-Black-4530-it.html https://www.bababorses.de/Givenchy-Antigona-Small-Leather-Satchel-Bag-Black-it-2432 https://www.bababorses.de/Louis-Vuitton-Monogram-Canvas-Small-Malle-Chain-Bag-3294-it.html https://www.bababorses.de/Louis-Vuitton-Epi-Leather-Zippy-Wallet-M62304-Red-7304-it.html https://www.bababorses.de/Louis-Vuitton-Iphone-Case-LV113-38-it.html https://www.bababorses.de/Louis-Vuitton-Heel-10.5cm-Eyeline-Pumps-Python-Pattern-Suede-Black-5999-it.html https://www.bababorses.de/LOUIS-VUITTON-PEGASE-LEGERE-REGATTA-N41620-MONOGRAM-CANVAS-6974-it.html https://www.bababorses.de/Louis-Vuitton-Kimono-Wallet-M56175-Pink-7437-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-Tapestry-Denim-Bidart-Espadrilles-Blue-5726-it.html https://www.bababorses.de/Louis-Vuitton-Women-s-Escale-Shirtdress-Blue-1709-it.html https://www.bababorses.de/Louis-Vuitton-Montaigne-MM-M41048-Black-4597-it.html https://www.bababorses.de/Louis-Vuitton-Heel-10cm-Crystals-Call-Back-Sandals-Suede-Red-6160-it.html https://www.bababorses.de/Hermes-Bolide-31cm-Togo-Leather-Green-Bag-it-1070 https://www.bababorses.de/Replica-Hermes-Wallet-H001-Wallet-Cow-Leather-Green-it-1558 https://www.bababorses.de/Celine-Medium-Luggage-Tote-Black-Brown-White-Bag-it-2168 https://www.bababorses.de/Louis-Vuitton-Pochette-Voyage-MM-Bag-Damier-Graphite-Canvas-Pixel-N60176-Green-7278-it.html https://www.bababorses.de/Fendi-Black-Snake-Veins-Leather-With-Beige-Ferrari-Leather-Top-handle-Bag-it-469 https://www.bababorses.de/Louis-Vuitton-All-over-Monogram-Sleeveless-Belted-Dress-Navy-1375-it.html https://www.bababorses.de/Louis-Vuitton-Ring-02-560-it.html https://www.bababorses.de/Louis-Vuitton-Iphone-Case-LV32-76-it.html https://www.bababorses.de/Louis-Vuitton-Dauphine-MM-M55071-Blue-4511-it.html https://www.bababorses.de/Louis-Vuitton-Supreme-Iphone-Case-White-Red-212-it.html https://www.bababorses.de/Louis-Vuitton-Epi-Smooth-Leather-Twist-Shoulder-Bag-MM-Pink-Black-2639-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-Empreinte-Leather-Cosmetic-Pouch-Bag-M80502-Bouton-de-Rose-Pink-By-The-Pool-Capsule-Collection-4296-it.html https://www.bababorses.de/Louis-Vuitton-Bracelet-21-271-it.html https://www.bababorses.de/Louis-Vuitton-Heel-9.5-cm-Star-Trail-Ankle-Boots-Black-5511-it.html https://www.bababorses.de/Louis-Vuitton-Geronimos-Belt-Bag-M43502-Black-Epi-Leather-2646-it.html https://www.bababorses.de/Louis-Vuitton-Damier-Ebene-Canvas-Vavin-Chain-Wallet-N60222-Bordeaux-Red-7221-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-Empreinte-Leather-Zippy-Coin-Purse-M80408-Cream-Saffron-By-The-Pool-Capsule-Collection-7741-it.html https://www.bababorses.de/Louis-Vuitton-Vintage-Monogram-Vernis-Bleecker-Box-Top-Handle-Bag-Burgundy-4172-it.html https://www.bababorses.de/Prada-Saffiano-Mini-Galleria-Crossbody-Bag-Beige-it-2708 https://www.bababorses.de/Louis-Vuitton-Sunglasses-39-1042-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-Canvas-Leopard-Print-Onthego-Tote-Bag-M44674-Black-White-3232-it.html https://www.bababorses.de/Louis-Vuitton-Epi-Leather-Twist-PM-Bag-with-Crystal-embellished-Chain-M55412-White-2630-it.html https://www.bababorses.de/Fendi-Chameleon-Red-Cross-Veins-Leather-Tote-Bag-it-488 https://www.bababorses.de/Louis-Vuitton-Monogram-Canvas-Onthego-Tote-Bag-M44571-Kaki-3270-it.html https://www.bababorses.de/Fendi-Earth-Yellow-Leather-with-Multicolor-Striped-Fabric-Shopping-Handbag-it-771 https://www.bababorses.de/Louis-Vuitton-Croco-Pattern-Petite-Boite-Chapeau-Bag-Black-4090-it.html https://www.bababorses.de/Prada-Saffiano-Small-Double-Handle-Tote-Bag-Light-Gray-Pomice-it-2849 https://www.bababorses.de/Louis-Vuitton-Lvxlol-Speedy-BB-M45202-Golden-3125-it.html https://www.bababorses.de/Louis-Vuitton-Gloria-Flat-Open-Back-Loafers-Monogram-Canvas-5798-it.html https://www.bababorses.de/LOUIS-VUITTON-BREA-PM-Monogram-Vernis-leather-IN-MORDORE-4074-it.html https://www.bababorses.de/Replica-Hermes-Steve-H2810-Ladies-Shoulder-Bag-Cow-Leather-it-1428 https://www.bababorses.de/Louis-Vuitton-Noe-bag-M42226-Brown-3496-it.html https://www.bababorses.de/Louis-Vuitton-Damier-Azur-Canvas-I-2260-it.html https://www.bababorses.de/Christian-Dior-Multicolor-PeachYellow-Zipper-Wallet-118-it-223 https://www.bababorses.de/Fendi-By-the-Way-Small-Tricolor-Satchel-Bag-it-2916 https://www.bababorses.de/Prada-Medium-Vitello-Diano-Open-Tote-Bag-Pomice-it-2728 https://www.bababorses.de/Luxury-Hermes-Wallet-H001-Unisex-Wallet-it-1598 https://www.bababorses.de/Louis-Vuitton-Iphone-Case-LV42-104-it.html https://www.bababorses.de/Prada-Saffiano-Small-Gardeners-Tote-Bag-Blue-it-2803 https://www.bababorses.de/Louis-Vuitton-Sac-Tricot-Bag-Epi-Leather-Red-M52805-2736-it.html https://www.bababorses.de/Louis-Vuitton-Crazy-in-Lock-Strass-Bracelet-Silver-316-it.html