js獲取當前頁面url信息


 

<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>

 

 

 

轉 : https://www.cnblogs.com/ysx215/p/12024621.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM