简体   繁体   English

外部HTML文件进入Flash / AS3中的“动态文本字段”

[英]External HTML file into Dynamic Text Field in Flash/AS3

I've looked through the forums, and couldn't find the answer to my question outright, so I figured I'd just ask it. 我浏览了各个论坛,但无法完全找到我的问题的答案,所以我想问一下就可以了。 Apologies if this question has been asked already. 如果已经提出这个问题,我们深表歉意。

I'm trying to load an external html file into a dynamic text field. 我正在尝试将外部html文件加载到动态文本字段中。 The dynamic text field has html display enabled. 动态文本字段已启用html显示。 The html file contains a list of links in tags, and nothing more. html文件包含标签中的链接列表,仅此而已。 The dynamic text field is called 'dynamictext', and I've tried: 动态文本字段称为“ dynamictext”,我尝试过:

var externaltextload:URLLoader = new URLLoader( );
externaltextload.load(new URLRequest("text/Links.html"));
externaltext.text=externaltextload.data;

and

var externaltextload:URLLoader = new URLLoader( );
externaltextload.load(new URLRequest("text/Links.html"));
externaltext.htmlText=externaltextload.data;

I'm probably way off with this. 我可能与此相去甚远。 Any help out there? 有什么帮助吗?

Thanks in advance! 提前致谢!

The URLLoader behaves asynchronously, meaning you can't expect an answer from the remote end immediately. URLLoader的行为是异步的,这意味着您不能期望远程端立即给出答案。 You need to listen for an event signalling that the data has been downloaded. 您需要侦听一个事件,该事件表明已下载数据。

Here's the example from the docs . 这是docs中的示例。 I don't think you'll find a better example. 我认为您找不到更好的例子。 Note how before the load operation, a load of listeners are registered with the URLLoader that will call the respective functions in the case of success or failure, in particular the line: 请注意,在装入操作之前,如何向URLLoader注册侦听器的装入,以在成功或失败的情况下调用相应的函数,尤其是以下行:

dispatcher.addEventListener(Event.COMPLETE, completeHandler); 

which hooks the COMPLETE event which is signalled when the data is ready. 钩住COMPLETE事件,该事件在数据准备好时发出信号。

package {
    import flash.display.Sprite;
    import flash.events.*;
    import flash.net.*;

    public class URLLoaderExample extends Sprite {
        public function URLLoaderExample() {
            var loader:URLLoader = new URLLoader();
            configureListeners(loader);

            var request:URLRequest = new URLRequest("urlLoaderExample.txt");
            try {
                loader.load(request);
            } catch (error:Error) {
                trace("Unable to load requested document.");
            }
        }

        private function configureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            dispatcher.addEventListener(Event.OPEN, openHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        }

        private function completeHandler(event:Event):void {
            var loader:URLLoader = URLLoader(event.target);
            trace("completeHandler: " + loader.data);

            var vars:URLVariables = new URLVariables(loader.data);
            trace("The answer is " + vars.answer);
        }

        private function openHandler(event:Event):void {
            trace("openHandler: " + event);
        }

        private function progressHandler(event:ProgressEvent):void {
            trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
        }

        private function securityErrorHandler(event:SecurityErrorEvent):void {
            trace("securityErrorHandler: " + event);
        }

        private function httpStatusHandler(event:HTTPStatusEvent):void {
            trace("httpStatusHandler: " + event);
        }

        private function ioErrorHandler(event:IOErrorEvent):void {
            trace("ioErrorHandler: " + event);
        }
    }
}

hmm, i tried Jose's method but doesnt seem to work! 嗯,我尝试了何塞的方法,但似乎没有用!

with an error it says: "The class or interface "Event" could not be loaded. The class or interface "URLLoader" could not be loaded. 出现错误,提示:“无法加载类或接口“ Event”。无法加载类或接口“ URLLoader”。

function displayHTML (e:Event):void { var xmlData = new XML(e.target.data); 函数displayHTML(e:Event):void {var xmlData = new XML(e.target.data);

These 2 sentences of code has some problems. 这两个代码语句有一些问题。

Sorry I'm a noob and your advise would be good! 抱歉,我是菜鸟,您的建议会很好!

You can't really load an HTML page into a text-field. 您实际上无法将HTML页面加载到文本字段中。 What you need to do is load an XML file that has HTML node and then display that string into the text-field, as that will render. 您需要做的是加载一个具有HTML节点的XML文件,然后将该字符串显示在文本字段中,以进行渲染。

Sample XML (myfile.xml): 样本XML(myfile.xml):

 <?xml version="1.0"?>
        <myhtml>
        <![CDATA[
        <a href="http://www.google.com">google</a><br />
        <a href="http://www.yahoo.com">yahoo</a><br />
        <a href="http://www.bing.com">bing</a><br />
        ]]>
        </myhtml>

Sample AS3: 示例AS3:

var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("myfile.xml"));
xmlLoader.addEventListener(Event.COMPLETE, displayHTML);

function displayHTML (e:Event):void
{
    var xmlData = new XML(e.target.data);
    //this is your text-field
    mytextfield.htmlText = xmlData.myhtml;
}

This should work, get back to me if you need more help, I wrote this on the fly, so there might be a typo here or there. 这应该可行,如果您需要更多帮助,请与我联系,我是即时写的,所以这里或那里可能有错字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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