两个纯Html之间的传值

来源:互联网 发布:win10网卡mac地址 编辑:程序博客网 时间:2024/05/18 05:33


摘要方法一:

http://blog.sina.com.cn/s/blog_664c8e480100izbw.html

index1.htm页面


<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<SCRIPT LANGUAGE="JavaScript">
    function show(){
        var result = document.getElementByIdx("name").value;
        location.href="index2.htm?name="+result;
    }
</SCRIPT>
<style>
.input7 {color: #999;width:145px;height:20px;border: 1px solid #CCCCCC; font-size:12px;background-color: #fff;}
</style>
</HEAD>

<BODY>
<input type="text" id="name" class="input7">
<input type="button" value="OK" onclick="show()"/>
</BODY>
</HTML>

 

index2页面:

<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<SCRIPT LANGUAGE="JavaScript">

function getvalue(name){
    var str=window.location.search;   //location.search是从当前URL的?号开始的字符串
                                         if (str.indexOf(name)!=-1){        
        var pos_start=str.indexOf(name)+name.length+1;
        var pos_end=str.indexOf("&",pos_start);
        if (pos_end==-1){
            alert( str.substring(pos_start));
        }else{
            alert("没有此值~~");
        }
    }

</SCRIPT>
</HEAD>

<BODY>
<input type="button" onclick="getvalue('show')" value="GetValue"/>
</BODY>
</HTML>


摘要方法二:

例如

a.htm:

1
2
3
4
<form action="b.htm" >
<input name="q" type="text" value=""  />
<input type="submit" value="提交" id="" />
</form>

b.htm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<html>
<body>
<div id="qbox"></div>
<script type="text/javascript">
function getArgs() { 
    var args = {};
        var query = location.search.substring(1);
         // Get query string
    var pairs = query.split("&"); 
                    // Break at ampersand
     for(var i = 0; i < pairs.length; i++) {
            var pos = pairs[i].indexOf('=');
             // Look for "name=value"
            if (pos == -1) continue;
                    // If not found, skip
                var argname = pairs[i].substring(0,pos);// Extract the name
                var value = pairs[i].substring(pos+1);// Extract the value
                value = decodeURIComponent(value);// Decode it, if needed
                args[argname] = value;
                        // Store as a property
        }
    return args;// Return the object 
 }
var str =getArgs();
alert(str['q']);//和input的name对应取值,
document.getElementById("qbox").innerHTML = str['q'];//然后赋值给DIV
</script>
</body>
</html>

0 0
原创粉丝点击