It is a useful feature to be able to pass values to a Flash application. For example, you can pass the value of the background color of the application, the default size of the window, initialize the states of components, etc, thus enabling you to cater to individual needs of your users. One way to accomplish this is by using the getInitArg method of the LzBrowser object.
For example, the code in Listing 13.4 prints the value of the firstName query parameter.
<canvas debug="true"> <script> Debug.write(LzBrowser.getInitArg("firstName")) ; </script> </canvas> |
To test the code, use the following URL to request the code:
http://localhost:8080/lps-4.0.x/app13/browserTest1.lzx?firstName=Ron
You will see the value of query parameter firstName printed in the Debugger window.
When working with URLs, you often work with the LzUrl class. Here are the methods:
parseURL(url)
Parses the specified string and returns a new LzURL object. You can then call the host, port, path, file, and query properties of the LzURL object.
toString()
Returns a string representation of this LzURL object.
For example, the LZX program in Listing 13.5 shows you how to work with the LzUrl class.
<canvas debug="true"> <script> var url = LzBrowser.getLoadURLAsLzURL(); Debug.write("protocol:" + url.protocol); Debug.write("host:" + url.host); Debug.write("port:" + url.port); Debug.write("path:" + url.path); Debug.write("file:" + url.file); Debug.write("query:" + url.query); </script> </canvas> |
To test the code, use the following URL to request the code:
http://localhost:8080/lps-4.0.x/app13/lzUrlTest1.lzx
If you run the program, you will see something like the following in the Debugger window.
protocol:http host:localhost port:8080 path:/lps-4.0.x/app13/ file:lzUrlTest1.lzx query:lzt=swf
Another use of the LzUrl class is to prevent piracy. For example, the code in Listing 13.6 always redirects the user to an error page (or any other page) if the Flash is loaded from any host other than localhost.
<canvas debug="true"> <script> var url = LzBrowser.getLoadURLAsLzURL(); if (url.host != "myHost.com") { LzBrowser.loadURL("http://www.mydomain.com/error.html"); } Debug.write("protocol:" + url.protocol); Debug.write("host:" + url.host); Debug.write("port:" + url.port); Debug.write("path:" + url.path); Debug.write("file:" + url.file); Debug.write("query:" + url.query); </script> </canvas> |