翼度科技»论坛 编程开发 JavaScript 查看内容

JavaScript中的location、history、navigator对象实例介绍

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
1.location
  1. //地址栏上#及后面的内容
  2. console.log(window.location.hash);
  3. //主机名及端口号
  4. console.log(window.location.host);
  5. //主机名
  6. console.log(window.location.hostname);
  7. //文件的路径---相对路径
  8. console.log(window.location.pathname);
  9. //端口号
  10. console.log(window.location.port);
  11. //协议
  12. console.log(window.location.protocol);
  13. //搜索的内容
  14. console.log(window.location.search);
  15. //设置跳转的页面的地址
  16. location.href = "//www.jb51.net";//属性----------------->必须记住
  17. location.assign("//www.jb51.net");//方法
  18. //重新加载--刷新
  19. location.reload();
  20. //没有历史记录
  21. location.replace("//www.jb51.net");
复制代码
2.history
  1. <body>
  2. <input type="button" value="跳过去" id="btn1"/>
  3. <input type="button" value="前进" id="btn2"/>
  4. <script>
  5.     //跳转的
  6.     document.getElementById("btn1").onclick = function () {
  7.         window.location.href = "http://www.baidu.com";
  8.     };
  9.     //前进
  10.     document.getElementById("btn2").onclick = function () {
  11.         window.history.forward();
  12.     };
  13. </script>
  14. </body>
复制代码
历史记录的后退和前进 history: back()后退 forward()前进
3.navigator
  1. <script>
  2.     //通过userAgent可以判断用户浏览器的类型
  3.     console.log(window.navigator.userAgent);
  4.     //通过platform可以判断浏览器所在的系统平台类型.
  5.     console.log(window.navigator.platform);
  6. </script>
复制代码
到此这篇关于JavaScript中的location、history、navigator对象实例介绍的文章就介绍到这了,更多相关js location、history、navigator内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

来源:https://www.jb51.net/javascript/284423.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具