When flex application loads in browser by default text input get focus.
By default a Flex application is not in focus in the browser when it loads. This can be especially frustrating if you have say a login view and would like the username field to be in focus when the application starts cranking. However, with some simple JavaScript we can set the Flash object to focus when the application loads.
NOTE: This will not work in Safari, Chrome, or any other browser that leverages Webkit, as it doesn’t allow the focus to be set on embedded objects. If you’re targeting IE and Firefox the proposed solution below will work.
For example purposes, let’s assume we want to make our username TextInput field have focus when the Flex app starts rocking.
First, set the focus to the username field — assume a ViewMediator is doing this:
Create a simple JavaScript method to set the focus of the browser to
your Flex application. Create this method in the index.template.html
file in your Flex project — just drop it right before the closing HTML
tag at the bottom of the file:
Next catch the application complete event and call the JS method we
just created — I put in how to remove the event handler for the app
complete for both Flex 3 and 4:
By default a Flex application is not in focus in the browser when it loads. This can be especially frustrating if you have say a login view and would like the username field to be in focus when the application starts cranking. However, with some simple JavaScript we can set the Flash object to focus when the application loads.
NOTE: This will not work in Safari, Chrome, or any other browser that leverages Webkit, as it doesn’t allow the focus to be set on embedded objects. If you’re targeting IE and Firefox the proposed solution below will work.
For example purposes, let’s assume we want to make our username TextInput field have focus when the Flex app starts rocking.
First, set the focus to the username field — assume a ViewMediator is doing this:
this
.view.userNameTextInput.setFocus();
<script type=
"text/javascript"
>
function
onFlexInitialized()
{
//alert("onFlexInitialized");
<!-- Force the browser to set flex app
with
focus -->
document.getElementById(
"${application}"
).focus();
}
</script>
/**
* Constructor.
*/
public
function
AppController()
{
FlexGlobals.topLevelApplication.addEventListener(FlexEvent.APPLICATION_COMPLETE, onAppComplete);
// Flex 4
//Application.application.addEventListener(FlexEvent.APPLICATION_COMPLETE, onAppComplete); // Flex 3
}
/**
* Handles the application complete event.
*/
protected
function
onAppComplete(e:FlexEvent):
void
{
FlexGlobals.topLevelApplication.removeEventListener(FlexEvent.APPLICATION_COMPLETE, onAppComplete);
// Flex 4
//Application.application.removeEventListener(FlexEvent.APPLICATION_COMPLETE, onAppComplete); // Flex 3
if
(ExternalInterface.available)
{
ExternalInterface.call(
"onFlexInitialized"
);
}
}
No comments:
Post a Comment