js获取Url信息

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script language="javascript" type="text/javascript">
        function aa() {
            //在本机测试
            var getHost = window.location.host;//路径(域名) localhost:8645
            var getAllUrl = window.location.href; //完整路径 http://localhost:/GetUrl.htm
            var gethttp = window.location.protocol; //Url的协议 http:
            var getprot = window.location.prot; //获取访问端口 使用默认80
            var getparhname = window.location.parhname; //获取文件的地址(url路径部分)
            var getSearh = window.location.search;//获取参数
            alert(getHost+","+getAllUrl+","+gethttp+","+getprot+","+getparhname+","+getSearh);
        }
   
    </script>
<!--    我们可以用javascript获得其中的各个部分
1, window.location.href
整个URl字符串(在浏览器中就是完整的地址栏)
2,window.location.protocol
URL 的协议部分
3,window.location.host
URL 的主机部分
4,window.location.port
URL 的端口部分
如果采用默认的80端口(update:即使添加了:80),那么返回值并不是默认的80而是空字符
本例返回值:""
5,window.location.pathname
URL 的路径部分(就是文件地址)
6,window.location.search
查询(参数)部分
除了给动态语言赋值以外,我们同样可以给静态页面,并使用javascript来获得相信应的参数值
7,window.location.hash
锚点-->
</head>

<body onload="aa();">

</body>
</html>

posted @ 2013-06-25 11:49  txbauo  阅读(560)  评论(0编辑  收藏  举报