HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Preload content in StageWebView

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
contentstagewebviewpreload

Problem

I would like to get some feedback on my AS3 code below. It's for an Adobe Air mobile app to preload a website in a StageWebView container.
That container will be moved on screen later in the app process. My goal is to show the website content to the user as fast as possible.

// OFF SCREEN - PRELOAD
var webView:StageWebView = new StageWebView();
webView.stage = this.stage;
webView.viewPort = new Rectangle( -5000, 0, stage.stageWidth, stage.stageHeight);
webView.loadURL("http://www.example.com/foobar.php");

// ONSCREEN
function showWeb(e:Event=null){
webView.viewPort = new Rectangle( 0, 0, stage.stageWidth, stage.stageHeight);
}

btn.addEventListener(MouseEvent.CLICK,  showWeb)


Is there something I could do better?
I'm using Adobe Air 3.6 and Flash CS6.

Thank yoo

Solution

I don't think there's anything that can be improved to your approach.

Minor improvements do apply;

function showWeb(e:Event=null){
webView.viewPort = new Rectangle( 0, 0, stage.stageWidth, stage.stageHeight);
}


could use some indentation,

btn.addEventListener(MouseEvent.CLICK,  showWeb)


contains a double space and is missing a semicolon.

But I don't think it's possible to preload the StageWebView via some function.

What you DO want to take care of is any errors that might be thrown your way.

For that, add a listener: (code from this SO answer)

webView.addEventListener(ErrorEvent.ERROR, onError);
function onError(e:ErrorEvent):void 
{
    trace("Page is not available.");
}

Code Snippets

function showWeb(e:Event=null){
webView.viewPort = new Rectangle( 0, 0, stage.stageWidth, stage.stageHeight);
}
btn.addEventListener(MouseEvent.CLICK,  showWeb)
webView.addEventListener(ErrorEvent.ERROR, onError);
function onError(e:ErrorEvent):void 
{
    trace("Page is not available.");
}

Context

StackExchange Code Review Q#30179, answer score: 2

Revisions (0)

No revisions yet.