×

HTML嵌入SWF教程

96
甘言川
2017.02.09 22:14* 字数 704
HTML嵌入SWF两种方式
  • 采用SWFObject.js(依赖swfobject.js)

    <script type="text/javascript"> 
      var swfVersionStr = "10.0.0"; 
      var xiSwfUrlStr = "playerProductInstall.swf"; 
      var flashvars = {}; 
      flashvars.firstname = "Nick"; 
      flashvars.lastname = "Danger"; 
      var params = {}; 
      params.quality = "high"; 
      params.bgcolor = "#ffffff"; 
      params.allowscriptaccess = "sameDomain"; 
      var attributes = {}; 
      attributes.id = "TestProject"; 
      attributes.name = "TestProject"; 
      attributes.align = "middle"; 
      //参数说明:embedSWF("SWF路径","嵌入的div的id","宽度","高度","SWF的版本号","player产品安装swf","给SWF的参数")
      swfobject.embedSWF( 
          "FlashVarTest.swf", "flashContent", "100%", "100%", swfVersionStr, 
          xiSwfUrlStr, flashvars, params, attributes); 
      swfobject.createCSS("#flashContent", "display:block;text-align:left;"); 
    </script>
  • 采用<object/>标签

    <object id='mySwf' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' height='100%' width='100%'>
          <param name='src' value='FlashVarTest.swf'/>
          <param name='flashVars' value='firstname=Nick&lastname=Danger'/>
          <embed name='mySwf' src='FlashVarTest.swf' height='100%' width='100%' flashVars='firstname=Nick&lastname=Danger'/>
      </object>
  • flashContent 是一个div
    <div id="flashContent">
      <p>
      当浏览器没有安装FlashPlayer的时候,显示此内容。(PS:一般在这里面加上FlashPlayer下载提示和下载地址(可以自定义内网地址))
      </p>
      <p>
      To view this page ensure that Adobe Flash Player version 
      ${version_major}.${version_minor}.${version_revision} or greater is installed. 
      </p>
      <script type="text/javascript"> 
      var pageHost = ((document.location.protocol == "https:") ? "https://" : "http://"); 
      document.write("<a href='http://www.adobe.com/go/getflashplayer'><img src='" 
              + pageHost + "www.adobe.com/images/shared/download_buttons/get_flash_player.gif' alt='Get Adobe Flash player' /></a>" ); 
      </script> 
    </div>
HTML 往SWF中传参数
  • SWObject方式

      var flashvars = {}; 
      flashvars.firstname = "Nick"; 
      flashvars.lastname = "Danger"; 
      swfobject.embedSWF( 
          "FlashVarTest.swf", "flashContent", "100%", "100%", "", 
          "", flashvars,{},{});
  • object标签方式

    <object id='mySwf' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' height='100%' width='100%'>
          <param name='src' value='FlashVarTest.swf'/>
          <!-- 传参数,必须满足URL编码格式 -->
          <param name='flashVars' value='firstname=Nick&lastname=Danger'/>
          <embed name='mySwf' src='FlashVarTest.swf' height='100%' width='100%' flashVars='firstname=Nick&lastname=Danger'/>
      </object>
  • 通用的URL传参方式
    //swfobject
    swfobject.embedSWF(
                  "FlashVarTest.swf?firstname=Nick&lastname=Danger",
                  "flashContent", "100%", "100%",
                  swfVersionStr, xiSwfUrlStr,
                  flashvars, params, attributes);
    //<object>
    <object id='mySwf' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' height='100%' width='100%'>
          <param name='src' value='FlashVarTest.swf?firstname=Nick&lastname=Danger'/>
    </object>
  • 服务器页面动态传参
    • JSP
      <%
      String fName = (String) request.getParameter("firstname");
      String lName = (String) request.getParameter("lastname");
      %>
      <script type="text/javascript">
              var swfVersionStr = "0";
              var xiSwfUrlStr = "";
              var flashvars = {};
              flashvars.firstname = "<%= fName %>";
              flashvars.lastname = "<%= lName %>";
              var params = {};
              params.quality = "high";
              params.bgcolor = "#ffffff";
              params.allowscriptaccess = "sameDomain";
              var attributes = {};
              attributes.id = "FlashVarTest";
              attributes.name = "FlashVarTest";
              attributes.align = "middle";
              swfobject.embedSWF(
                  "FlashVarTest.swf", "flashContent",
                  "100%", "100%",
                  swfVersionStr, xiSwfUrlStr,
                  flashvars, params, attributes);
          </script>
    • php
      <?php 
      @ $fName = $_GET['firstname']; 
      @ $lName = $_GET['lastname']; 
      ?> 
      <script type="text/javascript"> 
      var swfVersionStr = ""; 
      var xiSwfUrlStr = ""; 
      var flashvars = {}; 
      flashvars.firstname = "<?php echo $fName; ?>" 
      flashvars.lastname =  "<?php echo $lName; ?>" 
      var params = {}; 
      params.quality = "high"; 
      params.bgcolor = "#ffffff"; 
      params.allowscriptaccess = "sameDomain"; 
      var attributes = {}; 
      attributes.id = "FlashVarTest"; 
      attributes.name = "FlashVarTest"; 
      attributes.align = "middle"; 
      swfobject.embedSWF( 
          "FlashVarTest.swf", "flashContent", 
          "100%", "100%", 
          swfVersionStr, xiSwfUrlStr, 
          flashvars, params, attributes); 
      swfobject.createCSS("#flashContent", "display:block;text-align:left;"); 
      </script>
SWF 中获取参数
  • 获取指定参数值
    FlexGlobals.topLevelApplication.parameters.firstname
  • 通过for .. in遍历所有的属性名称和属性值
    for (var att:String in FlexGlobals.topLevelApplication.parameters) {
       trace( att + ":" + FlexGlobals.topLevelApplication.parameters[att] );
    }
    • Flex 中获取参数的完整示例
      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
      <fx:Script>
      <![CDATA[
      import mx.core.FlexGlobals;
      protected function button2_clickHandler(event:MouseEvent):void
      {
           log.text += "firstName:"+FlexGlobals.topLevelApplication.parameters.firstName +"\n";
      }
      protected function button3_clickHandler(event:MouseEvent):void
      {
         log.text += "=============开始遍历flashvar对象========\n";
         for (var i:String in FlexGlobals.topLevelApplication.parameters) {
                         log.text += i + ":" + FlexGlobals.topLevelApplication.parameters[i] + "\n";
         }
         log.text += "=============结束遍历flashvar对象========\n";
      }
      ]]>
      </fx:Script>
      <fx:Declarations>
      <!-- 将非可视元素(例如服务、值对象)放在此处 -->
      </fx:Declarations>
      <s:VGroup width="100%">
      <s:HGroup width="100%">
         <s:Button label="获取firstName参数值" click="button2_clickHandler(event)"/>
         <s:Button label="遍历参数中所有的属性名称和属性值" click="button3_clickHandler(event)"/>
      </s:HGroup>
      <s:Label text="输出日志:"/>
      <s:TextArea id="log" width="100%" />
      </s:VGroup>
      </s:Application>
Flex
Web note ad 1