zoukankan      html  css  js  c++  java
  • 使用JS获取当前页面的URL(网址信息)

    <!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>
    作者:赖忠标
    免责声明:文章、笔记等仅供分享、探讨、参考等学习之用,因此造成的任何后果概不负责。(如有错误、疏忽等问题,欢迎指正、讨论,谢谢)
    本文版权归作者和博客园共有,欢迎转载,但请务必在文章页面明显位置给出原文连接,谢谢配合。
  • 相关阅读:
    mysql定时器,定时查询数据库,把查询结果插入到一张表中 阿星小栈
    如何写mysql的定时任务 阿星小栈
    利用mysql游标循环结果集 阿星小栈
    页面可见生Page Visibility
    css之z-index
    css之页面三列布局之左右两边宽度固定,中间自适应
    css之页面两列布局
    jquery源码学习之extend
    jquery源码学习之queue方法
    HTTP状态码详解
  • 原文地址:https://www.cnblogs.com/lazb/p/14711676.html
Copyright © 2011-2022 走看看