別の Javascript が次を使用して挿入されます。 SwfObject SWF ファイル。 HTML ファイル内のどこにでも配置できます。最初に行う必要があるのは、適切な Flash Player がインストールされていない場合にユーザーにプロンプトを表示する DIV タグを作成することです。
try { var key:String; var val:String; var flashvars:Object = LoaderInfo(this.root.loaderInfo).parameters; for (key in flashvars) { val = String(flashvars[key]); variablesReceived.appendText("t" key ": " val " "); } } catch (error:Error) { variablesReceived.appendText(error.toString()); }
This will be The first text box lists all flashvars. Another main function we use in this file is the send variable function:
// Sending parameters function sendVariables(e:MouseEvent):void { // First we grab the URL of the HTML document and split it into an array var htmlUrl:String = ExternalInterface.call("window.location.href.toString"); // split the string at the questionmark var splitUrl:Array = htmlUrl.split("?"); // use only the first part (ditch existing parameters) var trimmedUrl:String = splitUrl[0]; // get the parameters we want to append to the URL var parameters:String = variablesToSend.text; // combine url and parameters with a new questionmark var requester:URLRequest = new URLRequest(trimmedUrl "?" parameters); // reload the page navigateToURL(requester, '_self'); }
Here we use a little trick to capture the URL of the HTML text inserted in the SWF file by using 'ExternalInterface.call'. Flash files only know the URL pointing to themselves. This technique breaks through this limitation. ExternalInterface is turned on by default in SwfObject, but you can turn it off manually.
We don’t need the parameters in the current URL (i.e. ‘…?test=something&id=5′). So we only kept the part before the question mark and stored it in the 'trimmedUrl' variable for future use. We capture the parameters in the 'variablesToSend' text box and pass them into the URLRequest. By passing the request to 'navigateToURL', the browser reloads the HTML page and displays the most recently submitted value pair in the 'variablesReceived' text box.
Note: You cannot test this in Flash. The file needs to be uploaded to the server because both FlashVars and ExternalInterface require the SWF to be inserted into the browser.
Finally we have to call the 'sendVariables' method for the send button settings using addEventListener. sendButton.addEventListener(MouseEvent.CLICK,sendVariables); Now you know how to pass parameters to each other using Javascript. Let's do something useful with what we've learned.
Create a navigation for record status Before finishing, let us build a small menu system that can highlight the currently clicked button, and you can download the completed file or run Case, let's take a look at the code: First stop the SWF timeline playback and set an event listener for mouse click. stop(); // setup our 5 buttons item1.addEventListener(MouseEvent.CLICK, gotoURL); item2.addEventListener(MouseEvent.CLICK, gotoURL); item3.addEventListener (MouseEvent.CLICK, gotoURL); item4.addEventListener(MouseEvent.CLICK, gotoURL); item5.addEventListener(MouseEvent.CLICK, gotoURL); When a button is clicked, they will execute the 'gotoURL' function . Next, we capture the parameters from the URL:
// grab variables try { var key:String; var val:String; var flashvars:Object = LoaderInfo(this.root.loaderInfo).parameters; for (key in flashvars ) { val = String(flashvars[key]); if(key == "item"){ // If the parameter is called 'item'... if(val.substr( 0,4) == "item"){ // ... and the name of the button starts with the characters 'item'... // ... we can extract the number-part of the item -name and go to the correct frame var frameToGoTo:Number = Number( val.substr(4,1) ); gotoAndStop( frameToGoTo 1 ); } } } } catch (error:Error) { // what to do if an error occurs }
As you can see, this is very similar to what we did before. But this time the parameter name we pass is 'item'. This parameter is the name of the button we clicked. Next is the gotoURL function.
// Get the new page function gotoURL( e:MouseEvent):void { // First we grab the URL of the HTML document and split it into an array var htmlUrl:String = ExternalInterface.call("window.location.href.toString"); // split the string at the questionmark var splitUrl:Array = htmlUrl.split("?"); // use only the first part (ditch existing parameters) var trimmedUrl:String = splitUrl[0]; // get the name of the button clicked and set it as a parameter var parameters:String = "item=" e.currentTarget.name; // combine url and parameters with a new questionmark var requester:URLRequest = new URLRequest(trimmedUrl "?" parameters); // reload the page navigateToURL(requester, '_self'); }
We create our own parameters by combining the 'item=' character with the name of the clicked button. Then pass the URL and parameters to the navigateToURL method to reload the HTML page with the new parameters.
How events work: When something is clicked we use the addEventListener() method to listen for click events, and the event contains a reference to the clicked object. The 'currentTarget' property refers to the clicked object (e.currentTarget), so we can use e.currentTarget.name to get its name.
To make it a full menu system, you would also need to load a new URL instead of using the same URL as in the example. You should know the basics by now. It can operate in multiple ways at the same time. You can store the URL as a variable in the SWF, load it from an XML file, or many other ways. So I leave these to you. If you create a solution using this tutorial, please post the URL in the comments so other learners can see it.