Flex - 调用JavaScript函数并打开显示数据的弹出窗口

时间:2013-06-04 21:38:24

标签: javascript html flash flex externalinterface

我一直试图解决这个问题......

我有一个Flex(Flash)应用程序,我称之为JavaScript函数并传入数据:

if (ExternalInterface.available)
ExternalInterface.call("jsFunctionToCalled", passingSomeData);

在我的index.template.html文件中,我创建了JS函数:

<script type="text/javascript">
function jsFunctionToCalled(value) {
window.alert(value);
}
</script>  

当我在Flex中单击一个Button组件时,会弹出该JS警告窗口。这很好;但是,我想打开一个浏览器窗口,在那里我可以访问“文件/打印”选项。我需要打开这个新的浏览器窗口并解析值对象中的数据。值对象是HTML格式的数据字符串。所以我需要在新的弹出窗口中显示该数据。我想也许我需要做这样的事情,有人在某处张贴,但没有任何东西弹出。我也试过window.opener,没有弹出任何东西。如果我提供了一个URL,它会打开URL,但我不想打开URL,我想打开一个可以打印的新窗口,并用HTML数据填充窗口。

<script>
function openWin()
{
myWindow=window.open('','','width=200,height=100');
myWindow.document.write("This is 'myWindow'!");
myWindow.focus();
myWindow.opener.document.write("<p>This is the source window!</p>");
}
</script>

任何帮助将不胜感激。我试图找到一种方法来打印,而不首先保存文件(CRAPPY FLASH),并且我没有用于保存文件的Web服务器,以避免首先保存文件。

由于

2 个答案:

答案 0 :(得分:1)

我想出了这个,并认为我会分享给其他遇到此问题的人:

在我的Flex代码中:

if (ExternalInterface.available)
            {
                try
                {
                    ExternalInterface.call("onPrintRequest", dataToPass);
                }
                catch (error:SecurityError)
                {
                    Alert.show("Printing Security Error");
                }
                catch (error:Error)
                {
                    Alert.show("Printing Error");
                }
            }
            else
            {
                Alert.show("Printing currently unavailable");
            }

在我的index.template.html中,我添加了这个JS方法:

 <script type="text/javascript">
            function onPrintRequest(value) {
                var w = window.open("about:blank");
                w.document.write(value);
                w.document.close();
                w.focus();
                w.print();
            }
        </script>

像魅力一样工作!!!

答案 1 :(得分:0)

以上更改非常有效。...

一项附加功能-如果要在打印后自动关闭新窗口,请在脚本中添加w.close()

 <script type="text/javascript">
 function onPrintRequest(value) {
            var w = window.open("about:blank");
            w.document.write(value);
            w.document.close();
            w.focus();
            w.print();
            w.close();
        }
</script>