javascript-常用内置对象-随堂

小结

js调试有三种方法,如果有一个变量m="hello",请使用三种方法完成m数值的显示
方法一,alert(m)
方法二,console.log(m)
方法三,document.title=m

document对象,获取当前页面是从哪一个网址跳转进来的
document.referrer

location对象,获取当前所在的页面地址
window.location.href

location对象,让页面跳转到这个网址:http://www.baidu.com
window.location.href="http://www.baidu.com"


location对象,获取url中get方式传递的参数
window.location.search


Math对象,生成随机数,获取一个0到1之间的随机小数
Math.random()

Math对象,向下取整。m="3.6",请获取m数据的向下取整值
Math.floor(m)

Math对象,向上取整,m="2.6",请获取m数据向上取整的值
Math.ceil(m)


一 js调试方法3种

alert

alert(值)

console.log

适合输出大量的结果

<script>
    for(var i=0;i<100;i++){
        console.log(i);
    }
</script>

document.tiitle

适只测试单个数据

<script>
    var name = "python";
    document.title=name;
</script>

二 document对象

获取来源

获取上一个页面的地址

document.referrer

应用场景
访问一个网站,匿名状态时,可以看到一个页面
比如
有一个商品吸引了我
需要登陆
希望登陆后,回到原来的页面
可以做的操作是

商品页—》登陆页,记录一下是从哪个页面过来的。
通过document.referrer,拿到上一个页面,并保存地址。
登陆成功后,跳转到记录的那个商品页面,用户可以继续操作了

在这里插入图片描述

三 location对象

1 获取当前所在的页面地址

window.location.href

2 页面跳转

window.location.href = 要跳转的页面地址

练习
点击一个按钮
点击后,跳转百度页面

增强版代码,山塞搜索引擎

<body>
<input type="text" id="ipt">
<button onclick="tz()">search</button>
<script>
    function tz() {
        var oIpt = document.getElementById("ipt")
        var sValue = oIpt.value
        var sUrl = "https://www.baidu.com/s?wd="
        var sSearchUrl = sUrl +sValue
        alert(sSearchUrl)
        window.location.href=sSearchUrl;
    }
</script>
</body>

界面
在这里插入图片描述

四 获取URL中传递的参数

用法

window.location.search

获得的是url中的参数部分

效果
在这里插入图片描述

熟悉一下url中的参数部分
在这里插入图片描述

在这里插入图片描述

获取页面中的锚点

window.location.hash

五 Math对象

1 生成随机数

0-1之间的浮点数

Math.random()

例子

<script>
    for(var i=0;i<100;i++){
    var num = Math.random();
    console.log(num)
}
</script>

结果
在这里插入图片描述

2 向下取整

用法

Math.floor(参数)

参数说明
参数为数字型,或者为数字型的字符串,都会获得整数部分
例如 3.1 或者 “3.1” 得到的结果是 3

英文
floor
在这里插入图片描述

例子

<script>
    alert(Math.floor(5.1));
</script>

3 向上取整

Math.ceil(参数)

参数说明
参数为数字型,或者为数字型的字符串,都会获得整数部分
例如 3.1 或者 “3.1” 得到的结果是 4

在这里插入图片描述
代码

<script>
    alert(Math.ceil("3.1"));
</script>

4 范围内的随机数

例子
在这里插入图片描述

效果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鲸鱼编程pyhui

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值