ziv's profile果子PhotosBlogLists Tools Help
    9/3/2007

    从网页中给 flash 传参数

    方法一:在网页中使用js,SetVariable设置flashobject中的变量,代码如:
    // "HtmlToSwf"为网页中的flashobject ID
    HtmlToSwf.SetVariable("_root.info_str","Happy Newyear");
    方法二:路径参数,如test.swf?foo=happy2005
    方法三:使用FlashVars,以下主要介绍FlashVars的用法。使用FlashVars后嵌入HTML的flashobject代码如下:
    <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="550" height="400" id="FlashVars" align="middle">
    <param name="allowScriptAccess" value="sameDomain" />
    <param name="movie" value="FlashVars.swf" />
    <param name="FlashVars" value="foo=happy2005&program=flash&language=简体中文-中国" />
    <param name="quality" value="high" />
    <param name="bgcolor" value="#ffffff" />
    <embed src="FlashVars.swf" quality="high" bgcolor="#ffffff" width="550" height="400" name="FlashVars" align="middle" allowScriptAccess="sameDomain" FlashVars="foo=happy2005&program=flash&language=简体中文-中国" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
    </object>
    //通过上面的代码,在SWF(FlashVars.swf)中就可以直接获取foo、program、language变量数据。FlashVars.fla获取FlashVars参数的代码如下:
    // 创建三个文本字段
    _root.createTextField("foo_txt",1,0,0,16,16);
    _root.createTextField("program_txt",2,0,32,16,16);
    _root.createTextField("language_txt",3,0,64,16,16);
    foo_txt.autoSize = true;
    foo_txt.border = true;
    program_txt.autoSize = true;
    program_txt.border = true;
    language_txt.autoSize = true;
    language_txt.border = true;
    // 获取FlashVars变量
    foo_txt.text = "HTML中的foo参数:"+foo;
    program_txt.text = "HTML中的program参数:"+program;
    language_txt.text = "HTML中的language参数:"+language;
    三、两者的有效结合。
    在HTML网页中使用js获取参数,然后将获取的参数作为FlashVars写入flashobject传递给swf。代码如下:
    <script. language=javascript>
    <!--
    function writeflashobject(parastr) {
    document.write("<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\" width=\"550\" height=\"400\" id=\"FlashVars\" align=\"middle\"\>\n");
    document.write("<param name=\"allowScriptAccess\" value=\"sameDomain\" /\>\n");
    document.write("<param name=\"movie\" value=\"FlashVars.swf\" /\>\n");
    document.write("<param name=\"FlashVars\" value=\""+ parastr +"\" /\>\n");
    document.write("<param name=\"quality\" value=\"high\" /\>\n");
    document.write("<param name=\"bgcolor\" value=\"#ffffff\" /\>\n");
    document.write("<embed src=\"FlashVars.swf\" quality=\"high\" bgcolor=\"#ffffff\" width=\"550\" height=\"400\" name=\"FlashVars\" align=\"middle\" allowScriptAccess=\"sameDomain\" FlashVars=\""+ parastr +"\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" /\>");
    document.write("</object\>");
    function getparastr() {
    var hrefstr,pos,parastr,para,tempstr1;
    hrefstr = window.location.href;
    pos = hrefstr.indexOf("?")
    parastr = hrefstr.substring(pos+1);
    return parastr;
    }
    var parastr = getparastr();
    writeflashobject(parastr);
    //-->
    </script> 

    Comments (1)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    No namewrote:
    To the global wow gold the cheapest wow power leveling under the cheapest single-site! -2140495084656
    Sept. 14

    Trackbacks

    The trackback URL for this entry is:
    http://onenature.spaces.live.com/blog/cns!3932EE522E2582D3!253.trak
    Weblogs that reference this entry
    • None