29
Aug
07

Launching new browser windows from Flex

The following example shows how you can use the navigateToURL() method in the flash.utils package to open new browser windows, or replace the content in the current window.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/29/launching-new-browser-windows-from-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import flash.net.navigateToURL;

            private function newWin(url:String):void {
                var urlRequest:URLRequest = new URLRequest(url);
                navigateToURL(urlRequest, String(comboBox.selectedItem));
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Label text="window:" />
        <mx:ComboBox id="comboBox">
            <mx:dataProvider>
                <mx:String>_blank</mx:String>
                <mx:String>_top</mx:String>
                <mx:String>CustomName</mx:String>
            </mx:dataProvider>
        </mx:ComboBox>
    </mx:ApplicationControlBar>

    <mx:LinkButton label="Go to adobe.com"
            click="newWin('http://www.adobe.com/')" />

</mx:Application>

View source is enabled in the following example.

In the previous example you can specify the target window by using the ComboBox control in the docked ApplicationControlBar container at the top. If you specify “_blank”, new windows will be created. If you specify “_top”, the current window will be replaced with the specified URL. If you specify the third option, “CustomName“, a new named window will be created. So clicking the LinkButton control 5 times loads the content into the same window. And of course the CustomName could be anything you wanted. Basically consider this the “target” attribute of an HTML <a> tag (as in <a href="http://www.adobe.com/" target="_blank">Go to adobe.com</a>).

When using the navigateToURL() method, it is important to remember that the method takes a URLRequest object, and not a String object.


9 Responses to “Launching new browser windows from Flex”


  1. 1 Xavi Nov 4th, 2007 at 10:14 pm

    Hello!

    I need some help because I’m trying to run the example but seems is not working

    _top is not working, only _blank it is

    I don’t know way

    Maybe is a bug ie7.

  2. 2 peterd Nov 4th, 2007 at 10:40 pm

    Xavi,

    What happens when you specify “_top” versus “_blank”? In the above example, specifying “_top” replaces the current page with the new URL, wheras pecifying “_blank” opens a new browser window in my IE7/WIN 9,0,60,235 (debug).

    Peter

  3. 3 fufach Jan 17th, 2008 at 2:41 pm

    Hi,

    In my Flex 3.3 _blank target is not working. I cannot open this ^ example with ‘blank’ as well. Is this a bug or there is a different way to open site in separate tab/window…?

    Fufach

  4. 4 Jana Feb 20th, 2008 at 8:06 am

    Hi,

    I’m interested in taking an xml:XML object in flex and launching a new browser window to display it. I wasn’t planning on saving it to a file but rather have created it dynamically in memory but want the browser to decide what XML viewer to invoke. Is there a way to do this without saving the file to disk or uploading it first to the server?

    Also…this web site (blog.flexexamples.com) is very hard to read in IE 7.0. The black color hides what I type in in the search input box. Anyway the designer could modify the colors of this website to be a little more user friendly? In particular I see light grey text in the black search text box. Very hard to see what I typed in.. Why black anyway? pretty hard on the eyes but then again I know that is very subjective…

    Jana

  5. 5 peterd Feb 20th, 2008 at 12:12 pm

    Jana,

    I think you would need to save the XML to the disk/server first.

    As for the blog design, yeah, this is just a default WordPress template. I’ve been thinking of finding a new theme as it seems there are lots of weird issues, like people not being able to post code in the comments without the blog somehow eating them.

    Peter

  6. 6 Devon Mar 7th, 2008 at 2:03 am

    Hey, when I try to open a window in _top or _self I get a security error:
    “SecurityError: Error #2137: Security sandbox violation”

    Any idea what could be the cause of this?
    Thanks

  7. 7 Ryan Aug 14th, 2008 at 11:27 am

    _top is the only part of this example that works for me. I’m using Firefox 3.0. I tested it in IE and it seems to work fine. Anyone for a way to do this for all browsers????

    Thanks

  8. 8 Teresa Sep 25th, 2008 at 9:32 pm

    Hi,

    How to hide the location bar and scroll bar in the new window?

    Thanks

  9. 9 peterd Sep 26th, 2008 at 12:22 am

    Teresa,

    I believe in order to hide location bars and scroll bars in a new HTML window, you need to use the ExternalInterface API and call a JavaScript function in your HTML container.

    Peter

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".




Badge Farm

  • Firefox 2
  • Powered by Redoable 1.2
  • Feeds burnt by Feedburner
  • Feed