javaScript页面间传值_使用网页对话框

在网站开发时,页面间传值是一个经常碰到的问题,传值的方法也有很多种,今天先说说使用javaScript在页面间传值;

1、 向打开的网页对话框传入简单类型

A页面代码:

 

<html>
<head><title></title></head>

    <body>
        <script type="text/javascript">
        
        function pass(){
            var temp = document.getElementById("t").value;
            window.showModalDialog("b.html",temp ,'dialogHeight:480px;dialogWidth:220px;status:0;help:0;edge:sunken;scroll:1;');
        }
        
         </script>
         <input type="text" id="t" /> 
         <input type="button" value="添  加" id="add_btn" οnclick="pass()"/>
     </body>
 </html>


B页面代码:

 

<html>
<head><title></title></head>

    <body>
        <script type="text/javascript">
        
        window.οnlοad=function() {
            var receive =window.dialogArguments;
            if(receive!=""){
               alert(receive);
            }
        }
        
         </script>
     </body>
 </html>

在A页面中定义需要被传递的变量 temp ,每次打开B页面时,将A页面上文本框的值赋予temp,通过window.showModalDialog传入B页面

2、  向打开的网页对话框传入自定义对象

A页面代码:

 

<html>
<head><title></title></head>

    <body>
        <script type="text/javascript">
        
        function Person(name,age,sex){
            this.name = name;
            this.age = age;
            this.sex = sex;
        }
        
        function pass(){
            if (InputIsNumber(document.getElementById("age").value)){
                 var person = new Person(document.getElementById("name").value,document.getElementById("age").value,document.getElementById("sex").value);
                 window.showModalDialog("b.html",person ,'dialogHeight:480px;dialogWidth:440px;status:0;help:0;edge:sunken;scroll:1;');
            }else{
                alert("年龄必须为数字");
            }
        }
        
        function InputIsNumber(input){
			var temp = "1234567890";
			var i;
			var j;
			for( i = 0; i < input.length; i ++ ){
				j = input.charAt( i );
			    if (temp.indexOf( j ) == -1){
					return false;
				}
			}
			return true;
		}
        
         </script>
         姓名:<input type="text" id="name" /> 
         年龄:<input type="text" id="age" /> 
         性别:<input type="text" id="sex" /> 
         <input type="button" value="添  加" id="add_btn" οnclick="pass()"/>
     </body>
 </html>

B页面代码:

 

<html>
<head><title></title></head>

    <body>
        <script type="text/javascript">
        
        window.οnlοad=function() {
            var receive =window.dialogArguments;
            if(receive!=""){
               document.write("姓名:"+receive.name+",年龄:"+receive.age+",性别:"+receive.sex);
            }
        }
        
         </script>
     </body>
 </html>

先在A页面中定义一个自定义对象Person,在A页面分别输入Person的姓名、年龄和性别,点击确认后将Person对象通过window.showModalDialog方法传入B页面,B页面在载入时接收该对象,同时直接通过对象的属性进行值访问;

3、  向打开的网页对话框输入自定义对象数组

A页面代码:

 

<html>
<head><title></title></head>

    <body>
        <script type="text/javascript">
        
        var PersonList = new Array(100);
        var index = 0;
        
        function Person(name,age,sex){
            this.name = name;
            this.age = age;
            this.sex = sex;
        }
        
        function pass(){
            window.showModalDialog("222.html",PersonList ,'dialogHeight:480px;dialogWidth:440px;status:0;help:0;edge:sunken;scroll:1;');
        }
        
        function add(){
            if (InputIsNumber(document.getElementById("age").value)){
                 var person = new Person(document.getElementById("name").value,document.getElementById("age").value,document.getElementById("sex").value);
                 PersonList[index] = person;
                 index++;
                 alert("已添加"+index+"个对象");
            }else{
                alert("年龄必须为数字");
            }
        }
        
        function InputIsNumber(input){
			var temp = "1234567890";
			var i;
			var j;
			for( i = 0; i < input.length; i ++ ){
				j = input.charAt( i );
			    if (temp.indexOf( j ) == -1){
					return false;
				}
			}
			return true;
		}
        
         </script>
         姓名:<input type="text" id="name" /> 
         年龄:<input type="text" id="age" /> 
         性别:<input type="text" id="sex" /> 
         <input type="button" value="添  加" id="add_btn" οnclick="add()"/>
         <input type="button" value="跳转页面" id="Button1" οnclick="pass()"/>
     </body>
 </html>

B页面代码:

 

<html>
<head><title></title></head>

    <body>
        <script type="text/javascript">
        
        window.οnlοad=function() {
            var receive =window.dialogArguments;
            for(var i =0 ; i<receive.length; i++){
                var temp = receive[i];
                if(temp!=null){
                    document.write("姓名:"+temp.name+",年龄:"+temp.age+",性别:"+temp.sex + "</br>");
                }
            }
        }
        
         </script>
     </body>
 </html>

在A页面中定义一个自定对象Person,以及一个空数组,每次一个Person后将新建的Person对象保存入数组,完成后通过window.showModalDialog方法将数组对象传入B页面,B页面接收页面后,通过访问数组的方法获取A页面传入的对象数组

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值