JavaScript-Runoob-JS 瀏覽器BOM :JavaScript Window Location
阿新 • • 發佈:2020-07-14
ylbtech-JavaScript-Runoob-JS 瀏覽器BOM :JavaScript Window Location |
1.返回頂部 |
JavaScriptWindow Location
window.location 物件用於獲得當前頁面的地址 (URL),並把瀏覽器重定向到新的頁面。
Window Location
window.location物件在編寫時可不使用 window 這個字首。 一些例子:
一些例項:
- location.hostname 返回 web 主機的域名
- location.pathname 返回當前頁面的路徑和檔名
- location.port 返回 web 主機的埠
- location.protocol 返回所使用的 web 協議(http: 或 https:)
Window Location Href
location.href 屬性返回當前頁面的 URL。
例項
返回(當前頁面的)整個 URL:
<script>document.write(location.href);
</script>
以上程式碼輸出為:
https://www.runoob.com/js/js-window-location.htmlWindow Location Pathname
location.pathname 屬性返回 URL 的路徑名。
例項
返回當前 URL 的路徑名:
<script>document.write(location.pathname);
</script>
以上程式碼輸出為:
/js/js-window-location.htmlWindow Location Assign
location.assign() 方法載入新的文件。
例項
載入一個新的文件:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鳥教程(runoob.com)</嘗試一下 » 2、title> </head> <head> <script> function newDoc(){ window.location.assign("https://www.runoob.com") } </script> </head> <body> <input type="button" value="載入新文件" onclick="newDoc()"> </body> </html>
2.返回頂部 |
3.返回頂部 |
4.返回頂部 |
5.返回頂部 |
6.返回頂部 |
作者:ylbtech 出處:http://ylbtech.cnblogs.com/ 本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線,否則保留追究法律責任的權利。 |