ActionScript 3.0. Developers now have to use the new navigateToURL method to load a web page from a Flex application.The navigateToURL method loads a document from a specified URL. You can use it to load the document in the same or a new browser window, and you can optionally pass variables with the URL.you can also use the method to call a JavaScript function on the web page hosting the SWF file. You can use the navigateToURL method in the following way:
navigateToURL(request, window);
The first argument, request, is a URLRequest object that specifies which web page to load. The
window argument determines how the page loads, and it can be one of the following string values:
_self, _blank, _parent, or _top.
To open the google web site in a new web browser window from a Flex application, you could
use the following lines of ActionScript:
var site:URLRequest = new URLRequest("http://www.google.com");
navigateToURL(site, "_blank");
Sending variables when calling a web page
Some Flex applications need to send variables to the web page that hosts the SWF file.
Whatever your need, one approach is to use the navigateToURL method to pass the values. There are two ways to do this:
1. Append variables to the URL, as in a GET operation.
2. Use a URLVariables object with POST.
You can add querystring variables from Flex by referring to the variable name at the end of the URL, as shown here:
var isbn:String = "1590597338";
var request:URLRequest =
new URLRequest("http://www.friendsofed.com/book.html?isbn=" + isbn);
This has the same effect as using HTTP GET to send form variables.
You can also create a new URLVariables object and pass the variables from Flex that way. The following example shows how to POST username and password variables with a URLRequest:
var site:URLRequest = new URLRequest("http://www.friendsofed.com");
var urlVars:URLVariables = new URLVariables();
site.method = "POST";
urlVars.username = "sas";
urlVars.password= "1234";
site.data = urlVars;
navigateToURL(site);
you’ll need to import the relevant classes, including the following:
import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.net.URLVariables;
You can also use the URLVariables class to send values to a server-side script.
Calling JavaScript with navigateToURL
Instead of loading a web page, you can call a JavaScript function with the navigateToURL method. To do this, don’t specify a URL. Use navigateToURL to reference a javascript command or function on the page containing the SWF file:
var jsRequest:URLRequest = new URLRequest("javascript: doWork();");
The preceding line calls the doWork JavaScript function in the web page that hosts the SWF file. Be aware that this approach might not work in all browsers, and security settings might prevent the JavaScript code from executing. A more robust approach is to use the External API.
Sending variables into a SWF file
In some cases, the hosting web page needs to send values into a Flex application. You might do this if you used server-side code to log in and you want to indicate to the Flex application that the user has logged in privileges. You could also use values from the web page to load a specific Flex application state.
there are two main approaches to sending variables into a SWF file so they can be used in your application:
Using querystring parameters when loading the SWF file
Using flashVars
Using querystring parameters
Querystring parameters are name-value variable pairs that are added at the end of a URL or file name;
for example:
mySWF.swf?userID=1234&admin=true
Once you add these parameters to the end of the SWF file name, they are accessible as variables in your Flex application.
No comments:
Post a Comment