javascript中怎么使用window.location对象实现页面跳转(javascript,window.location,web开发)

时间:2024-05-03 04:22:15 作者 : 石家庄SEO 分类 : web开发
  • TAG :

接下来,请跟着小编一起来学习吧!

javascript中怎么使用window.location对象实现页面跳转

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&#39;)
}
</script>
</body>
</html>

使用javascript实现页面定时跳转---location对象

要求实现以下效果:

javascript中怎么使用window.location对象实现页面跳转

代码实现思路:

  • 编写定时跳转的HTML页面。

  • 获取指定的秒数,并减1写入页面。

  • 当秒数大于0时,利用setTimeout()循环倒计时。

  • 当秒数小于等于0时,利用location.href跳转到指定的URL地址中。

实现代码如下:

html代码:

javascript中怎么使用window.location对象实现页面跳转

css代码:

javascript中怎么使用window.location对象实现页面跳转

js代码:

javascript中怎么使用window.location对象实现页面跳转

实现效果:

javascript中怎么使用window.location对象实现页面跳转

若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

本文:javascript中怎么使用window.location对象实现页面跳转的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:javascript window.screen对象如何应用下一篇:

12 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18