网页间的传值
網頁間的傳值有三種方法,一是通過地址欄;二是通過cookie;三是直接調用處理,即通過window.opener獲取具體的參數;這種方法比較靈活。
父頁面:
<html>
<head><title>網頁間的參數傳遞[父網頁]</title>
<script language="javascript">
??? function test1(){
??????? window.open('child.html','popup',200,300)
??? } function test2(){
??????? SetCookie('參數1',document.all.參數1.value,123);
??????? SetCookie('參數2',document.all.參數2.value,123);
??????? window.open('child.html','popup',200,300)
??? } function SetCookie(Para,Value,Expiredays){
??????? var Exdate=new Date()
??????? Exdate.setDate(Exdate.getDate()+Expiredays)
??????? document.cookie=Para+ "=" +escape(Value)+((Expiredays==null) ? "" : ";expires="+Exdate.toGMTString())
??? } function test3(){
??????? var Url="child.html?參數1="+escape(document.all.參數1.value);
??????? Url+="&參數2="+escape(document.all.參數2.value)
??????? window.open(Url,'popup',200,300)
??? }
</script>
</head>
<body>
<div id=PSP style="width:300;height:300;top=50;left=50;">
參數1:<input type="text" name="參數1" /></br>
參數2:<input type="text" name="參數2" /></br>
<input type="button" value="傳值" οnclick="test1();"/>
<input type="button" value="Cookie傳值" οnclick="test2()"/>
<input type="button" value="地址欄傳值" οnclick="test3()"/>
</div>
</body>
</html>
子頁面:
<html>
<head><title>網頁間的傳值[子網頁]</title>
</head>
<script languang="javascript">
??? function ViewInfo1() {
??????? var ParentW = window.opener;
??????? info="參數1:"+ParentW.document.all.參數1.value;
??????? info+="??? 參數2:"+ParentW.document.all.參數2.value;
??????? ParentW.document.getElementById('參數1').style.backgroundColor = "red";//更改背景色
??????? alert(info);
??? } function GetCookie(Para){
??????? if (document.cookie.length>0){
??????????? c_start=document.cookie.indexOf(Para + "=");
??????????? if (c_start!=-1){
??????????????? c_start=c_start + Para.length+1 ;
??????????????? c_end=document.cookie.indexOf(";",c_start);
??????????????? if (c_end==-1) c_end=document.cookie.length;
??????????????? return unescape(document.cookie.substring(c_start,c_end));
??????????? }
??????? }
??? }
???
??? function ViewInfo2(){
??????? var Para1=GetCookie('參數1');
??????? var Para2=GetCookie('參數2');
??????? var info="參數1:"+Para1+"??? 參數2:"+Para2;
??????? alert(info);
??? } function ViewInfo3(){
??????? var Url=location.search;
??????? var ParaName=new Array();
??????? var ParaValue=new Array();
??????? var info='';
??????? if(Url.indexOf("?")!=-1){
??????????? var S1 = Url.substr(1);
??????????? var SSS = S1.split("&");
??????????? for(var i=0;i<SSS.length;i++){
??????????????? ParaName[i]=SSS[i].split("=")[0];
??????????????? ParaValue[i]=SSS[i].split("=")[1];
??????????? }
??????? }
??????? for(var i=0;i<ParaName.length;i++){
??????????? if(info==''){info=ParaName[i]+"="+ParaValue[i];}
??????????? else {
??????????? info+="??? "+ParaName[i]+"="+ParaValue[i];}
??????? }
??????? alert(info) ; }
</script>
<body>
<input type="button" value="直接獲取參數" οnclick="ViewInfo1();"/>
<input type="button" value="cookie傳參數" οnclick="ViewInfo2();"/>
<input type="button" value="地址欄傳參數" οnclick="ViewInfo3();"/>
</body>
</html>
父頁面:
<html>
<head><title>網頁間的參數傳遞[父網頁]</title>
<script language="javascript">
??? function test1(){
??????? window.open('child.html','popup',200,300)
??? } function test2(){
??????? SetCookie('參數1',document.all.參數1.value,123);
??????? SetCookie('參數2',document.all.參數2.value,123);
??????? window.open('child.html','popup',200,300)
??? } function SetCookie(Para,Value,Expiredays){
??????? var Exdate=new Date()
??????? Exdate.setDate(Exdate.getDate()+Expiredays)
??????? document.cookie=Para+ "=" +escape(Value)+((Expiredays==null) ? "" : ";expires="+Exdate.toGMTString())
??? } function test3(){
??????? var Url="child.html?參數1="+escape(document.all.參數1.value);
??????? Url+="&參數2="+escape(document.all.參數2.value)
??????? window.open(Url,'popup',200,300)
??? }
</script>
</head>
<body>
<div id=PSP style="width:300;height:300;top=50;left=50;">
參數1:<input type="text" name="參數1" /></br>
參數2:<input type="text" name="參數2" /></br>
<input type="button" value="傳值" οnclick="test1();"/>
<input type="button" value="Cookie傳值" οnclick="test2()"/>
<input type="button" value="地址欄傳值" οnclick="test3()"/>
</div>
</body>
</html>
子頁面:
<html>
<head><title>網頁間的傳值[子網頁]</title>
</head>
<script languang="javascript">
??? function ViewInfo1() {
??????? var ParentW = window.opener;
??????? info="參數1:"+ParentW.document.all.參數1.value;
??????? info+="??? 參數2:"+ParentW.document.all.參數2.value;
??????? ParentW.document.getElementById('參數1').style.backgroundColor = "red";//更改背景色
??????? alert(info);
??? } function GetCookie(Para){
??????? if (document.cookie.length>0){
??????????? c_start=document.cookie.indexOf(Para + "=");
??????????? if (c_start!=-1){
??????????????? c_start=c_start + Para.length+1 ;
??????????????? c_end=document.cookie.indexOf(";",c_start);
??????????????? if (c_end==-1) c_end=document.cookie.length;
??????????????? return unescape(document.cookie.substring(c_start,c_end));
??????????? }
??????? }
??? }
???
??? function ViewInfo2(){
??????? var Para1=GetCookie('參數1');
??????? var Para2=GetCookie('參數2');
??????? var info="參數1:"+Para1+"??? 參數2:"+Para2;
??????? alert(info);
??? } function ViewInfo3(){
??????? var Url=location.search;
??????? var ParaName=new Array();
??????? var ParaValue=new Array();
??????? var info='';
??????? if(Url.indexOf("?")!=-1){
??????????? var S1 = Url.substr(1);
??????????? var SSS = S1.split("&");
??????????? for(var i=0;i<SSS.length;i++){
??????????????? ParaName[i]=SSS[i].split("=")[0];
??????????????? ParaValue[i]=SSS[i].split("=")[1];
??????????? }
??????? }
??????? for(var i=0;i<ParaName.length;i++){
??????????? if(info==''){info=ParaName[i]+"="+ParaValue[i];}
??????????? else {
??????????? info+="??? "+ParaName[i]+"="+ParaValue[i];}
??????? }
??????? alert(info) ; }
</script>
<body>
<input type="button" value="直接獲取參數" οnclick="ViewInfo1();"/>
<input type="button" value="cookie傳參數" οnclick="ViewInfo2();"/>
<input type="button" value="地址欄傳參數" οnclick="ViewInfo3();"/>
</body>
</html>
總結
- 上一篇: RHEL/CENTOS 性能优化
- 下一篇: {在头值中找到无效的字符。} 发emai