简体   繁体   中英

AS3 Flash - render html via ExternalInterface

This is my first question on Stack Overflow. It's not the first time I wanted to write one, but usually I find a solution using the search bar, this time I didn't. The problem I'm tackling is a little bit complex so I'll try and be as thorough as possibile.

Basically we're adding Chinese payments to an already existing ecommerce in Flash. The whole website is in AS3, embedded using SWFObject, already using ExternalInterface for other things.

This new Chinese payment method is a bit old-fashioned, so they have a strange way of handling payments. Once we've sent a POST to their servers with all the order details, they respond with an HTML page. My problem is rendering this page, considering that I receive it inside Flash.

The solution I'm trying at the moment works partially, meaning that I'm able to see the page, but the Chinese characters that are supposed to be in the page are rendering out badly. Instead of the Chinese characters I'm seeing weird characters, so I'm guessing there must be an encoding problem when I pass the HTML from Flash to Javascript. This is how I'm doing it:

AS3:

//extract html page from response
var newHTML:String = e.currentTarget.data;
//trim whitespace to avoid javascript error
newHTML = newHTML.replace(/\n/g, '');
newHTML = newHTML.split("\r").join("");

if(ExternalInterface.available)
  ExternalInterface.call("chinesePayment('"+newHTML+"')");
else
  trace("External interface error");

Javascript:

function chinesePayment(param) {
  var newWindow = window.open();
  //var unescaped = unescape(param);
  newWindow.document.write(param);
}

I've tried messing about with unescape, escape, URIencoding but without any success, so I'm really hoping you can help me out here!

Thanks, Domenico

EDIT:

I'd just like to mention that I'm receiving a correct HTML page from their servers. I've tried saving the page locally, copying the HTML code directly from the server response, and the page views correctly. That means that there has to be something wrong in the process of passing the page from AS3 to Javascript.

EDIT2 !important:

I've realised that the problem lays in the popup encoding. When I copy the HTML from the popup, paste it in an editor and save it I can correctly view the HTML. Seems like the popup doesn't consider gbk encoding. I'm now looking for a solution to this problem.

I've finally found a solution!!!!

Basically I had to render the chinese characters in flash so that when I passed them to javascript they were already encoded. So first of all I had to change the type of URLLoaderDataFormat to BINARY so that I received a byteArray:

my_loader.dataFormat = URLLoaderDataFormat.BINARY;

Once I received the response I modified the code this way:

        var bytes:ByteArray = e.currentTarget.data;
        var newHTML:String = bytes.readMultiByte(bytes.length,"gb2312");
        //trim whitespace to avoid javascript error
        newHTML = newHTML.replace(/\n/g, '');
        newHTML = newHTML.split("\r").join("");

        if(ExternalInterface.available)
            ExternalInterface.call("chinesePayment('"+newHTML+"')");
        else
            trace("External interface error");

As you can see, thanks to a specific byteArray function I can read the response using the preferred charset, and now it works!

The javascript remained the same, without unescape or similars. In this way the javascript function received a string with the chinese characters in it, not with the equivalent utf chars.

Thanks to everyone, you helped me get to the solution!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM