<!DOCTYPE HTML>
<html>
<head>
<title>事件处理</title>
<meta charset="utf-8" />
</head>
<body>
<form>
<input name="username" placeholder="请输入用户名"/>
<input type="password" name="pwd" placeholder="请输入密码"/>
<input type="submit"/>
</form>
<!--2. 实现不能后退的页面跳转-->
<button >go to tmooc</button>
<script>
function search2Obj(){
var result={};//创建空结果对象result
//如果location的search不为空字符串
if(location.search){
//将location的search选取1位置后的剩余内容,再按&切割,保存在props中
var props=
location.search.slice(1).split("&")
//遍历props中每个子字符串
for(var i=0;i<props.length;i++){
//将当前子字符串按=切割,结果保存在arr中
var arr=props[i].split("=");
//为result对象添加新属性,属性名为arr中第一个元素,属性值为arr中第二个元素
result[arr[0]]=arr[1];
}//(遍历结束)
}
return result;//返回result
}
//?username=zhangdong&pwd=123456
//console.dir(search2Obj());
/*
{
username:zhangdong,
pwd:123456
}
*/
var params=search2Obj();
console.log(params.username);
</script>
</body>
</html>
<html>
<head>
<title>事件处理</title>
<meta charset="utf-8" />
</head>
<body>
<form>
<input name="username" placeholder="请输入用户名"/>
<input type="password" name="pwd" placeholder="请输入密码"/>
<input type="submit"/>
</form>
<!--2. 实现不能后退的页面跳转-->
<button >go to tmooc</button>
<script>
function search2Obj(){
var result={};//创建空结果对象result
//如果location的search不为空字符串
if(location.search){
//将location的search选取1位置后的剩余内容,再按&切割,保存在props中
var props=
location.search.slice(1).split("&")
//遍历props中每个子字符串
for(var i=0;i<props.length;i++){
//将当前子字符串按=切割,结果保存在arr中
var arr=props[i].split("=");
//为result对象添加新属性,属性名为arr中第一个元素,属性值为arr中第二个元素
result[arr[0]]=arr[1];
}//(遍历结束)
}
return result;//返回result
}
//?username=zhangdong&pwd=123456
//console.dir(search2Obj());
/*
{
username:zhangdong,
pwd:123456
}
*/
var params=search2Obj();
console.log(params.username);
</script>
</body>
</html>