Ask a Jedi: Opening a new window from a Flash Form

Tim asks:

Michael White posted the question of a button in a Flash form that opens up a new window. How would you implement this? I know adding the javascript function to an "onClick" doesnt work.

So, onClick is definitely how to start. I built code for this for my Lighthouse Pro project. Let's look at the code (which can probably be done better) and I'll explain what I did...

<script>
function show(u) {
      if(u != '') window.open(u, 'sub', 'directories=yes, location=yes, menubar=yes, resizable=yes, scrollbar=yes, status=yes, titlebar=yes, toolbar=yes')
}
</script>


<cfform format="flash" name="foo" width="400" height="200">
   <cfformgroup type="horizontal">
      <cfinput type="text" validate="url" name="theURL" width="150" message="Use a proper URL">
      <cfinput type="button" name="showURL" value="Load in New Window" width="150"
            onClick="getURL('javascript:show(\''+theURL.text+'\')')">

   </cfformgroup>
</cfform>

I start off with the code I'll use to open a new window. This is your fairly standard "window.open" code. It may - in fact - get blocked by pop up blockers. My Firefox never complained though. Anyway, that being said, the code is pretty standard. Unlike most popups, I don't specify a height and width. This tends to let the browser use the defaults, which is the same size as the user's main window.

Next comes our flash form. I didn't know where you wanted the new window to go, so I added a simple text input box for a URL. The button is what we really care about. For some reason it took me numerous tries to get all the escaping and string parsing done right here. Basically we use getURL, which is a Flash method that lets you talk to JavaScript. I'm calling my "show" JavaScript function defined above, and passing in the value of the text input box. Obviously you don't have to have a text input box. Maybe it is a static URL, or something from a grid, but you get the idea.

Archived Comments

Comment 1 by David Retsid posted on 10/12/2005 at 11:48 PM

Hi,

Your sample code worked fine (thanks!), except where theURL.txt (in my case a search term to append to a URL) contains an apostrophe. Like in the Book Title "Frankie's Run". I've tried modifying the code above to include a call to escape(theURL.txt) but it doesn't seem to work either. Any suggestions?

BTW Your window open code has a bug. it should list "scrollbars=yes" not "scrollbar=yes"

Comment 2 by Raymond Camden posted on 10/13/2005 at 1:10 AM

I'm assuming you need to escape it some how. Maybe look in the AS docs for replacing single quotes. Sorry - not sure how else.

Comment 3 by Michael White posted on 2/9/2006 at 8:52 PM

I made a comment to the asfusion blog and Ed Welch gave this response (which is easier if you don't need to control the window):
onclick="if (gridContacts.selectedItem.ContactID > 0 ) {getUrl('ContactInfo.cfm?ContactID=' + gridContacts.selectedItem.ContactID,'_blank');}"

Comment 4 by Michael White posted on 3/15/2006 at 10:55 PM

ok, here's a dumb question: once you have your javascript window open just how you like it... what's the easy way to close it down by clicking a "cancel" button?

Comment 5 by Raymond Camden posted on 3/15/2006 at 11:01 PM

Why wouldn't you just hit the X button in the upper right hand corner?

Comment 6 by Adam Cockman posted on 5/22/2006 at 6:42 PM

Hi,

I am trying to open a new window without toolbars address bar and as a certain size from a flash form, Is it possible to do this on <a href> tag instead of being on a button???

Ads

Comment 7 by Bill Cupps posted on 8/18/2006 at 12:58 AM

I have a long cfform, the scroll bars show and work just fine. But what I am looking for is the ability to keep whatever control the user has focus on within the viewable area as the user tabs through the form. Any ideas?

Comment 8 by Dan O&aposKeefe posted on 11/1/2006 at 9:44 PM

Similar to Adam, I am finding it impossible to get a popup window from a straight href like you would use on an html form. When I try to use an inage and the onclick event, I cannot remove the border around the image (Flash form makes it look like an image embedded in a button.