Document.location

25 篇文章 0 订阅

Document.location 是一个只读属性,返回一个 Location 对象,包含有文档的 URL 相关的信息,并提供了改变该 URL 和加载其他 URL 的方法。

尽管 Document.location 是一个只读的 Location 对象,你也能够赋给它一个 DOMString。这意味着你能够赋给 document.location 字符串,大多数情况下像这样使用:document.location = ‘http://www.example.com’,也可写为document.location.href = ‘http://www.example.com’。

只是想获取字符串形式的 URL,可以使用只读属性 document.URL。

If the current document is not in a browsing context, the returned value is null.

Document.location用法示例:

<html>
     <head>
         <title>document.location </title>
     </head>
     <body>
         <b>document.location使用举例 </b ><p>
          <script type ="text/javascript"> 
       
             document.write( " 当前位置: " + document.location + " <p> " );
             document.write( " 当前位置的url路径: " + document.location.pathname + " <p> " );    
             document.write( " 当前位置的url协议: " + document.location.protocol + " <p> " );
             document.write( " 当前位置的url端口: " + document.location.port + " <p> " );
             // 设置新的location 
             //document.location.href= " http://www.baidu.com " ;
             window.open('http://www.baidu.com');
          </script>
     </body>
 </html> 

运行后,打印结果是:

document.location使用举例

当前位置: http://localhost/yacol/js/document.html

当前位置的url路径: /yacol/js/document.html

当前位置的url协议: http:

当前位置的url端口:

document.location.href= " http://www.baidu.com " ;//转到百度
window.open(‘http://www.baidu.com’);//在新窗口打开百度

参考自:
https://developer.mozilla.org/zh-CN/docs/Web/API/Document/location

https://www.cnblogs.com/wangkongming/archive/2013/03/08/2950321.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值