JavaScript学习笔记(2)

来源:互联网 发布:映客花椒类型直播源码 编辑:程序博客网 时间:2024/06/09 07:05
Window Location
在编写时可不使用 window 这个前缀
location.hostname 返回 web 主机的域名
location.pathname 返回当前页面的路径和文件名
location.port 返回 web 主机的端口 (80 或 443)
location.protocol 返回所使用的 web 协议(http:// 或 https://)
例子:
 
 <script>        document.write("当前页面的URL:" + location.href+"<br/>");        document.write("路径和文件名:"+location.pathname+"<br/>");        document.write("域名:"+location.hostname+"<br/>");        document.write("所使用的协议:" + location.protocol + "<br/>");        document.write("主机的端口:"+location.port);  </script>


location.assign() 方法加载新的文档
加载一个新的文档例子:
<head><script>function newDoc() { location.assign("http://www.baidu.com") }</script></head><body><input type="button" value="加载新文档" onclick="newDoc()"></body>



Window History
在编写时可不使用 window 这个前缀
history.back()  与在浏览器点击后退按钮相同
history.forward()  与在浏览器中点击按钮向前相同
后退和前进按钮的例子:
创建2个网页
第一个做向前:
<head>    <script>        function goForward() {            window.history.forward()        }    </script></head><body>    <a href="HtmlPage1.html">跳转</a> //href=你的第2个网站的名字    <input type="button" value="前进" onclick="goForward()"></body>


第二个做后退:
<head><script>function goBack(){    window.history.back()}</head><body></script><input type="button" value="后退" onclick="goBack()"></body>


0 0
原创粉丝点击