1. 程式人生 > >js獲取當前頁面url信息

js獲取當前頁面url信息

href 端口 proto toc 127.0.0.1 style last art length

```    
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script> var url; url = window.location.href; /* 獲取完整URL */ alert(url); /* http://127.0.0.1:8020/Test/index.html#test?name=test */ url = window.location.pathname; /* 獲取文件路徑(文件地址) */ alert(url); /* /Test/index.html */ url = window.location.protocol; /* 獲取協議 */ alert(url); /* http */ url = window.location.host; /* 獲取主機地址和端口號 */ alert(url); /* http://127.0.0.1:8020/ */ url = window.location.hostname; /* 獲取主機地址 */ alert(url); /* http://127.0.0.1/ */ url = window.location.port; /* 獲取端口號 */ alert(url); /* 8020 */ url = window.location.hash; /* 獲取錨點(“#”後面的分段) */ alert(url); /* #test?name=test */ url = window.location.search; /* 獲取屬性(“?”後面的分段) */ alert(url); /* 如果需要URL中的某一部分,可以自己進行處理 */ url = window.location.pathname; url = url.substring(url.lastIndexOf(‘/‘) + 1, url.length); alert(url); /* /index.html */ /* * 如果頁面使用了框架(frameset) * 要獲取到指定頁面的URL * 只要把window換成指定的頁面即可 */ /* ‘frame‘為指定頁面的class名 */ var url = window.parent.frames[‘frame‘].location.href; /* 獲取當前地址欄中顯示的URL */ var url = window.parent.location.href; /* window parent 可互換 */ var url = parent.window.location.href; </script> </head> <body> </body> </html>

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">

```

js獲取當前頁面url信息