javascript中怎么使用window.location对象实现页面跳转
导读:本文共1971.5字符,通常情况下阅读需要7分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要:接下来,请跟着小编一起来学习吧!window.location对象用于获得当前页面的地址(URL),并把浏览器重定向到新的页面。window location 对象在编写时可不使用Window这个前缀。一些例子:location.hostname返回web主机的域名location.pathname返回当前页面的路径和文件名location.port返回web主机的端口location.proto... ...
目录
(为您整理了一些要点),点击可以直达。接下来,请跟着小编一起来学习吧!
window.location对象用于获得当前页面的地址(URL),并把浏览器重定向到新的页面。
window location 对象在编写时可不使用Window这个前缀。一些例子:
location.hostname返回web主机的域名
location.pathname返回当前页面的路径和文件名
location.port返回web主机的端口
location.protocol返回所使用的的web协议
window location href
示例
返回当前页面的URL:
<scripttype="text/javascript">
document.write(location.href);
</script>
以上代码输出为:
http://127.0.0.1:8848/7.11/new_file.html
window location pathname
location.pathname属性返回URL的路径名。
示例
返回当前URL的路径名:
<scripttype="text/javascript">
document.write(location.pathname);
</script>
以上代码输出为:
/7.11/new_file.html
window location Assign
location.assign()方法加载新的文档。
示例
加载一个新的文档:
<!DOCTYPEhtml>
<html>
<head>
<metacharset="utf-8">
<title></title>
</head>
<body>
<inputtype="button"name=""id=""value="跳转到百度"onclick="newDoc()"/>
<scripttype="text/javascript">
functionnewDoc(){
window.location.assign('https://www.baidu.com')
}
</script>
</body>
</html>
使用javascript实现页面定时跳转---location对象
要求实现以下效果:
代码实现思路:
编写定时跳转的HTML页面。
获取指定的秒数,并减1写入页面。
当秒数大于0时,利用setTimeout()循环倒计时。
当秒数小于等于0时,利用location.href跳转到指定的URL地址中。
实现代码如下:
html代码:
css代码:
js代码:
实现效果:
若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!javascript中怎么使用window.location对象实现页面跳转的详细内容,希望对您有所帮助,信息来源于网络。