Skip to main content
Known Participant
July 1, 2010
Question

Opening an HTML popup from Flash, not as tab

  • July 1, 2010
  • 4 replies
  • 5139 views

Hi

I need to open an HTML popup from Flash (using ActionScript in
Flex 4).

Thus far I found a solution, and it does work in Safari - a new
and separate HTML window is popped up (with a new web page).


private function popUpTheHtmlEditor():void {
  var url:String = "http://www.adobe.com";
  var request:URLRequest = new URLRequest(url);
  try {
    navigateToURL(request, '_blank');
  } catch (e:Error) {
    trace("Error occurred!");
  }
}

But in (my) Firefox (with my settings), when I click on the Flash
(Flex) button, the page is being opened in a new tab.

I need a solution which pops up the HTML page in a new/separate
window, as far as possible regardless of the browser (and browser
settings).

I found an example where a popup is opened from HTML, and it opens a
separate window (a real popup) in my Firefox:
http://www.gtalbot.org/FirefoxSection/Popup/PopupAndFirefox.html
-> "Open a requested popup"

The JavaScript code seems to be:

  OpenRequestedPopup(this.href, this.target); return false;

with this function def:

  function OpenRequestedPopup(strUrl, strTarget)
    // ...
    if (WindowObjectReferenceOfRequestedPopup == null || WindowObjectReferenceOfRequestedPopup.closed)
    {
    WindowObjectReferenceOfRequestedPopup = window.open(strUrl, strTarget, "top=" + windowTop + ",left=" + windowLeft + ",width=" + windowWidth + ",height=" + windowHeight + ",menubar,toolbar,location,resizable,scrollbars,status");
    }
    else
    {
    if(WindowObjectReferenceOfRequestedPopup.focus)
      {
      WindowObjectReferenceOfRequestedPopup.focus();
      };
    };
    //...
  }

How could I call such code from ActionScript? Should this work through
ExternalInterface? I couldn't even get this to work:

  ExternalInterface.call('alert', 'foo');

Should I use swfobject.js for embedding the .swf in the HTML wrapper?

Any other ideas?

The two files are pasted below.

Here's the compiler command I'm using:

  mxmlc -output button.swf -target-player 10.0.0 flash_to_editor.mxml

I'm running the examples locally (as files, not over http). Would it
help to place the files on a server?

Tobi

<?xml version="1.0" encoding="UTF-8"?>
<mx:Application
  xmlns:fx="http://ns.adobe.com/mxml/2009"
  xmlns:s="library://ns.adobe.com/flex/spark"
  xmlns:mx="library://ns.adobe.com/flex/mx"
  horizontalAlign="center" verticalAlign="middle">
  <fx:Script>
    <![CDATA[

import mx.controls.Alert;
import flash.external.ExternalInterface;
import flash.net.URLRequest;

// This 'http://www.adobe.com' is just an example URL.

// Works but opens tab in FF (not a separate window):
private function popUpTheHtmlEditor():void {
  var url:String = "http://www.adobe.com";
  var request:URLRequest = new URLRequest(url);
  try {
    navigateToURL(request, '_blank');
  } catch (e:Error) {
    trace("Error occurred!");
  }
}

// Doesn't work:
// var url:URLRequest = new URLRequest("javascript:alert('foo'); void(0);");
// navigateToURL(url, "_self");

/*
// How to get this to work?
private function popUpTheHtmlEditor():void {
  ExternalInterface.call('alert', 'foo');
}
*/

/*
// How to get this to work?
private function popUpTheHtmlEditor():void {
  ExternalInterface.call('launch','http://www.adobe.com');
  // Or
  // ExternalInterface.call("window.open", "http://www.adobe.com", "win", "height=200,width=300,toolbar=no,scrollbars=yes");
}
*/

/*
// Perhaps it's necessary to try different approaches:
// (As soon as I get two approaches to work.)
private function popUpTheHtmlEditor():void {
  var s:String;
  if (ExternalInterface.available) {
    // Necessary? Safe?:
    // Security.allowDomain('*');
    s = ExternalInterface.call('launch','http://www.adobe.com');
  } else {
     // TODO: Then try URLRequest?
     s = "Wrapper not available";
  }
  Alert.show(s);
*/

    ]]>
  </fx:Script>
  <s:Panel title="One way to open the HTML editor"
  width="75%" height="75%">
    <s:Button id="button" label="Open the HTML editor"
    click="this.popUpTheHtmlEditor();"
    horizontalCenter="0" verticalCenter="0"/>
  </s:Panel>
</mx:Application>

<html
  xmlns="http://www.w3.org/1999/xhtml"
  xml:lang="en" lang="en">
  <head>
    <title>html_around_swf.html</title>
    <script type="text/javascript">
function launch(url) {
  alert(url);
  //  OpenWin = this.open(url, "FOO", "toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no,width=400,height=200");
  //}
}
    </script>
  </head>
  <body>
    <object id="button" name="button" width="550" height="400"
    classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000">
      <param name="movie" value="button.swf" />
      <param name="allowScriptAccess" value="always" />
      <embed id="foo" name="button" src="button.swf" width="550" height="400"></embed>
    </object>
  </body>
</html>

This topic has been closed for replies.

4 replies

tobi3Author
Known Participant
July 8, 2010

P.S.

Setting the value of allowScriptAccess to sameDomain might be safer.

Tobi

tobi3Author
Known Participant
July 8, 2010
Hi,

Here's a solution.

1. Make ExternalInterface work when files are loaded as local files:

Go to
http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html

In tab Global Security Settings: Edit Locations -> Add Location ->
browse to the .swf file, add it. Make sure it's in the list (under the
Edit Locations drop down, with a green check mark.)

Now open the HTML wrapper in a browser (eg Firefox).

Now this works (ActionScript):

private function popUpTheHtmlEditor():void {
  ExternalInterface.call('alert', 'foo');
}

On Windows: perhaps:
http://techjig.blogspot.com/2008/03/flash-global-security-settings-windows.html
"In order for external interface functions to be called, you must also
specify the folder/file where the swf file is located."

The above setup isn't necessary when the files are on a server.

Also see:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html#call()
""ExternalInterface" "
"call() method"

http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf69084-7ea6.html
"About ExternalInterface API security in Flex"

http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf626ae-7fe8.html#WS2db454920e96a9e51e63e3d11c0bf69084-7f31
"Using the ExternalInterface API to access JavaScript"

2. Open a real popup: Example:

ActionScript:

private function popUpSomething():void {
  var editorPathOrUrl:String =
    'wrapper.html';
  var windowName:String =
    "SomeNameSeeDocs";
  var windowFeatures:String =
    "toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes,width=500,height=700";
  ExternalInterface.call(
    'launch',editorPathOrUrl,windowName,windowFeatures
  );
}

JavaScript:

function launch(editorPathOrUrl,windowName,windowFeatures) {
  windowRef = window.open(
    editorPathOrUrl,windowName,windowFeatures
  );
}

Works in all browser I tested except Opera.

Tobi
tobi3Author
Known Participant
July 6, 2010

Found a solution, will post it later.

Tobi

Kartik_Mehta
Inspiring
July 2, 2010
tobi3Author
Known Participant
July 4, 2010

Hi

I went to

http://apdevblog.com/problems-using-navigatetourl/

-> "Here is also a working example: navigateToURL  example."

-> http://apdevblog.com/examples/navigatetourl/

Clicking on either button brings a new tab (instead of a popup).

How I could I run JavaScript code like this code here?

  http://www.gtalbot.org/FirefoxSection/Popup/PopupAndFirefox.html
  ->  "Open a requested popup"

How to get ExternalInterface to work?

Tobi