a.html中有一个链接
1
<a href="b.html?x=2&y=3">进入b.html</a>
可以使用到js,如下:
a.htm:
1
2
3
4
<form action="b.htm" >
<input name="q" type="text" value="" />
<input type="submit" value="提交" id="" />
</form>
b.htm
<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 = 0i <pairs.lengthi++) {
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>
希望能帮到你哦!
使用Cookie传递参数 ,a页面保存Cookie,b页面读取,代码如下:
a页面
<html><head>
<title>a</title>
<style type="text/css">
* {margin:0}
body {text-align:centermin-width:760px}
div {padding:3px 3px 3px 3px}
#main {width:720pxmargin:0 autotext-align:leftmargin-top:30px}
#main div span {width:50px}
</style>
<script type="text/javascript">
/***
* @param {string} cookieName Cookie名称
* @param {string} cookieValue Cookie值
* @param {number} nDays Cookie过期天数
*/
function SetCookie(cookieName,cookieValue,nDays) {
/*当前日期*/
var today = new Date()
/*Cookie过期时间*/
var expire = new Date()
/*如果未设置nDays参数或者nDays为0,取默认值1*/
if(nDays == null || nDays == 0) nDays = 1
/*计算Cookie过期时间*/
expire.setTime(today.getTime() + 3600000 * 24 * nDays)
/*设置Cookie值*/
document.cookie = cookieName + "=" + escape(cookieValue)
+ "expires=" + expire.toGMTString()
}
function login() {
var username = $("user").value
var password = $("pass").value
/*是否选中7天内无需登录*/
var save = $("save").checked
if(username=="abc" && password=="abc") {
if(save) SetCookie("username",username,7)
else SetCookie("username",username,1)
/*跳转到ex8.html页面*/
document.location = "b.htm"
} else {
alert("用户名或密码错误!")
}
}
function $(id) {
return document.getElementById(id)
}
</script>
</head>
<body>
<div id="main">
<div><span>用户名:</span><input type="text" id="user" /></div>
<div><span>密码:</span><input type="password" id="pass" /></div>
<div>
<input type="checkbox" id="save" />
7天内无需登录
<input type="button" onclick="login()" value="登录" />
</div>
</div>
</body>
</html>
b页面
<html><head>
<title>b</title>
<script type="text/javascript">
/***
*读取指定的Cookie值
*@param {string} cookieName Cookie名称
*/
function ReadCookie(cookieName) {
var theCookie = "" + document.cookie
var ind = theCookie.indexOf(cookieName)
if(ind==-1 || cookieName=="") return ""
var ind1 = theCookie.indexOf('',ind)
if(ind1==-1) ind1 = theCookie.length
/*读取Cookie值*/
return unescape(theCookie.substring(ind+cookieName.length+1,ind1))
}
function $(id) {
return document.getElementById(id)
}
function init() {
var username = ReadCookie("username")
if(username && username.length>0) {
$("msg").innerHTML = "<h1>欢迎光临," + username + "!</h1>"
} else {
$("msg").innerHTML = "<a href='a.htm'>请登录</a>"
}
}
</script>
</head>
<body onload="init()">
<div id="msg"></div>
</body>
</html>
效果如下: