HTML DOM href 属性

定义和用法

href 属性是一个可读可写的字符串,可设置或返回当前显示的文档的完整 URL。

因此,我们可以通过为该属性设置新的 URL,使浏览器读取并显示新的 URL 的内容。

语法

  1. location.href=URL

实例

假设当前的 URL 是: http://example.com:1234/test.htm#part2:

  1. <html>
  2. <body>
  3.  
  4. <script type="text/javascript">
  5. document.write(location.href);
  6. </script>
  7.  
  8. </body>
  9. </html>

输出:

  1. http://example.com:1234/test.htm#part2