js获取html参数及向swf传递参数应用介绍

我们知道HTML页面是在客户端执行的,这样要获取参数必须使用客户端脚本(如JavaScript),在这点上不同于服务器端脚本获取参数方式。

下面的这段js代码获取HTML网页形如"test.html?foo=mytest&program=flash" "?"后所有参数。

复制代码 代码如下:


<script language=javascript>
<!--
var hrefstr,pos,parastr;
hrefstr = window.location.href;
pos = hrefstr.indexOf("?");
parastr = hrefstr.substring(pos 1);
if (pos>0){
document.write("所有参数:" parastr);
} else {
document.write("无参数");
}
//-->
</script>


一、下面的这段js代码则可以更加细化获取HTML网页某一参数

复制代码 代码如下:


<script language=javascript>
<!--
function getparastr(strname) {
var hrefstr,pos,parastr,para,tempstr;
hrefstr = window.location.href;
pos = hrefstr.indexOf("?") 字串9
parastr = hrefstr.substring(pos 1);
para = parastr.split("&");
tempstr="";
for(i=0;i<para.length;i )
{
tempstr = para[i];
pos = tempstr.indexOf("=");
if(tempstr.substring(0,pos) == strname) {
return tempstr.substring(pos 1);
}
}
return null;
}
// 获取program参数
var programstr = getparastr("program");
document.write(programstr);
//-->
</script>


二、在HTML网页中向swf传递参数
方法一:在网页中使用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"> 字串5
<param value="sameDomain" />
<param value="https://www.jb51.net/FlashVars.swf" />
<param value="foo=happy2005&program=flash&language=简体中文-中国" />
<param value="high" />
<param value="#ffffff" />
<embed src="https://www.jb51.net/FlashVars.swf" quality="high" bgcolor="#ffffff" allowScriptAccess="never" allowNetworking="internal" autostart="0" FlashVars="foo=happy2005&program=flash&language=简体中文-中国" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />


通过上面的代码,在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;
字串8
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=https://www.jb51.net/article/\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=https://www.jb51.net/article/\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\" width=https://www.jb51.net/article/\"550\" height=https://www.jb51.net/article/\"400\" id=https://www.jb51.net/article/\"FlashVars\" align=https://www.jb51.net/article/\"middle\"\>\n");
document.write("<param name=https://www.jb51.net/article/\"allowScriptAccess\" value=https://www.jb51.net/article/\"sameDomain\" /\>\n");
document.write("<param name=https://www.jb51.net/article/\"movie\" value=https://www.jb51.net/article/\"FlashVars.swf\" /\>\n"); 字串2
document.write("<param name=https://www.jb51.net/article/\"FlashVars\" value=https://www.jb51.net/article/\"" parastr "https://www.jb51.net/article/\" /\>\n");
document.write("<param name=https://www.jb51.net/article/\"quality\" value=https://www.jb51.net/article/\"high\" /\>\n");
document.write("<param name=https://www.jb51.net/article/\"bgcolor\" value=https://www.jb51.net/article/\"#ffffff\" /\>\n");
document.write("<embed src=https://www.jb51.net/article/\"FlashVars.swf\" quality=https://www.jb51.net/article/\"high\" bgcolor=https://www.jb51.net/article/\"#ffffff\" width=https://www.jb51.net/article/\"550\" height=https://www.jb51.net/article/\"400\" name=https://www.jb51.net/article/\"FlashVars\" align=https://www.jb51.net/article/\"middle\" allowScriptAccess=https://www.jb51.net/article/\"sameDomain\" FlashVars=https://www.jb51.net/article/\"" parastr "https://www.jb51.net/article/\" type=https://www.jb51.net/article/\"application/x-shockwave-flash\" pluginspage=https://www.jb51.net/article/\"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>

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wddwxp.html