JavaScript高级程序设计(第四版)笔记location.search的用法

跳过了十一章,promise部分难以理解。
十二章BOM,location对象,一开始也是一头雾水,书本上只是讲述了方法,缺少实际的应用,说实话没明白location里面各个属性到底有什么用。后来网上看了黑马的视频,才有了更深的理解。
location的属性方法,基本上都是对浏览器地址栏的信息进行一些处理。

例如location.href,可以得到当前网页的url
到百度的页面,控制台输入location.href就可以得到百度地址
在这里插入图片描述
location.search,主要是用于两个网页之间信息的传递。
在这里插入图片描述
这里建立了两个html文件,index.html和login.html
我要的效果是,在login.html网页中,输入用户名,这个用户名可以在index.html中显示
这是login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="index.html">
      <label>用户名:</label> <input type="text" name="uname">
      <input type="submit" value="登录">
    </form>
</body>
</html>

这是index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div></div>
</body>
<script>
    console.log(location.search);
    let params= location.search.substr(1);
    console.log(params);
    //利用‘=‘把字符串分割为数组split('=')
    let arr = params.split('=');
    console.log(arr);
    var div = document.querySelector('div');
    //把数据写入DIV中 decodeURIComponent解码
    div.innerHTML = decodeURIComponent(arr[1]);
</script>
</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值