HTML from external file in MovieClip? - actionscript-3

I have a client that is requesting an area be built on the stage of an existing .fla file (that I built as well) that loads html content from an external file. This area will serve as a dynamic ad banner that displays the contents of the external file. Ideally, the external file would contain links to different .html files that can easily be edited as the content needs change.
I have tried HTMLloader, but I am limited to a .swf file only and cannot use AIR.
Any thoughts on the best way to do this?
Thanks in advance!

If you are in fact needing to pull content from an external .html file, the following code showcases how to load external html content. In my example, I stored some content in a file named 'MyBannerAd.html', but you can point it to wherever you want and whatever filename you wish. Once the file has loaded, the externalFileLoadComplete function is called at which time I created a textfield and stored the contents of the file that was loaded from the external file as the htmlText property of the TextField. If you already have a textfield created, you can simply reference that one instead. The code shown below executed successfully using Flash CC 2014.
import flash.text.TextField;
function externalFileLoadComplete(e:Event):void
{
var myTextField:TextField = new TextField();
myTextField.htmlText = e.target.data;
addChild(myTextField);
}
var fileLoader:URLLoader = new URLLoader();
var fileRequest:URLRequest = new URLRequest("MyBannerAd.html");
fileLoader.addEventListener(Event.COMPLETE, externalFileLoadComplete);
fileLoader.load(fileRequest);

Related

Load HTML file through file naming in Android Studio

I need to load a random image, according to the outcome and based on that I need to load a certain HTML file. Can I link them through naming?
When I load a drawable house.jpg it has to link dynamically with house.html.
(The image and HTML file have to be separate. So no need to say you can put everything in one HTML file.)
You can only do such a thing if our image is inside your /assets folder. Also, you must load your html with a baseUrl that's inside your assets folder
You can use WebView.loadUrl() or WebView.loadDataWithBaseURL():
webView.loadUrl("file:///android_asset/file.html");
or
webView.loadDataWithBaseURL("file:///android_asset/", "<img src='file.jpg' />", "text/html", "utf-8", null);
(file.jpg should be inside your assets folder)
To compare which button :
Use getDrawable() method in ImageButton and compare them using .getConstantState().equals()
Sample code:
ImageButton btn = (ImageButton) findViewById(R.id.myImageBtn);
Drawable drawable = btn.getDrawable();
if (drawable.getConstantState().equals(getResources().getDrawable(R.drawable.myDrawable).getConstantState())){
//Do your work here
}
References:
Get drawable of image button

LOCAL HTML file to generate a text file

I am trying to generate a TEXT/XML file from a LOCAL HTML file. I know there are a lot of answers to generating a file locally, usually suggesting using ActiveX object or HTML 5.
I'm guessing there is a way to make it work on all browsers (in the end HTML extension is opened by a browser even if it is a LOCAL file) and easily since this is a LOCAL file put in by user himself.
My HTML file will be on client's local machine not accessed via HTTP.
It is basically just a form written in HTML that upon "SAVE" command should be generating an XML file in the local disk (anywhere user decides) and saving form's content in.
Any good way?
One way that I can think of is, the html form elements can be set into class variables and then using the jaxb context you can create an XML file out of it.
Useful Link: http://www.vogella.com/tutorials/JAXB/article.html
What you can do is use base64 data-urls (no support for IE9-) to download the file:
First you need to create a temporary iframe element for your file to download in:
var ifrm = document.createElement('iframe');
ifrm.style.display = 'none';
document.body.appendChild(ifrm);
Then you need to define what you want the contents of the file to download to be, and convert it to a base64 data-url:
var html = '<!DOCTYPE html><html><head><title>Foo</title></head><body>Hello World</body></html>';
htmlurl = btoa(html);
and set it as source for the iframe
ifrm.src = 'data:text/x-html;base64,'+htmlurl;

In swf AS3, how do you extract string content from a website

In swf AS3, how do you extract string content from a website?
In a html web page, they display a single line of text. In the swf, I want to use as3 to access that page by its URL and retrieve that line. Is the a way to access the content?
Thanks in advance for your help!
You simply load the file and read it. Say you have this HTML located at domain.com/test:
<!DOCTYPE html>
<html>
<body>
<div class="target">Get this text</div>
</body>
</html>
You would load it in using a URLLoader in Flash:
var l:URLLoader = new URLLoader();
l.addEventListener(Event.COMPLETE, completeHandler);
l.load(new URLRequest("domain.com/test"));
That will load the above HTML in as a String in the completeHandler via Event.target.data. You can then do two things: Search for your string via RegEx or search for it by setting the HTML as an XML object.
Using RegEx, you would do it like this:
function completeHandler(e:Event):void {
var s:String = e.target.data;
var targets:Array = s.match(/(?<=<div class="target">).*(?=<\/div>)/igm);
}
targets would be an array of matches for a div with class name "target".
XML would be a bit more difficult, but infinitely more flexible and easy to maintain (in my opinion, at least). I won't give an example for how to do that, since parsing XML is a very common thing in AS3 and there are tons of other questions out there about it.
Hopefully that helps

How to add image in the library

I have the image path manipulate from database with php. Now how can i have to import those image in the library so that i can use them as a symbol. Thanks in advance.
You cannot load it into library, but you can load this image via php and use it on stage with action script. Take a look at this answer to the similar question:
Load image dynamicly in flash
You can download it to the server and generate source file with Class that have field like this:
class Library
{
[Embed(source = 'path_to_folder/image_name.png')] public static var imageClass : Class;
}
Alsou you should refer this class somewhere to be sure it will be compiled into your swf and use it like this:
const image:Bitmap = new (Library.imageClass)();
In that case you need to recompile your swf every time the image is changed.
But I'm totaly agree with Bartek that it is better to use dynamic loading.

pass a base path to a swf loaded at runtime

I have a main.swf which loads a module.swf and the module.swf loads some assets.
The module.swf works standalone and also needs to work when loaded by main.swf.
But unfortunately the module.swf can't find the assets when loaded by main.swf because the assets aren't located relative to the main.swf, but are located relative to the module.swf.
As I can't touch the module.swf and I'm also unwilling to change the directory structure, I am looking for a solution close to the a "base" parameter which can be used when a swf is embedded into html.
Is there a way to simulate the base parameter's behaviour when loading a swf file using Loader?
Here is a similar yet unanswered question.
You could use symlinks as a way to redirect your assets url when loaded via the main swf
Something like this
public class URLUtil
{
public static function getBaseURL(swfURL:String):String
{
return swfURL.substr(0, swfURL.lastIndexOf("/") + 1);
}
}
example of usage inside module:
var assetURL:String = URLUtils.getBaseURL(root.loaderInfo.url) + "asset.filename";
loader.load(new URLRequest(assetURL));