window.location

来源:互联网 发布:程序员工具 编辑:程序博客网 时间:2024/05/08 15:13


window.location方法获取URL   

统一资源定位符 (Uniform Resource Locator, URL)   
完整的URL由这几个部分构成:scheme://host:port/path?query#fragment   
scheme:通信协议就是常用的http协议、ftp,maito等   
host:主机,服务器(计算机)域名系统 (DNS) 主机名或 IP 地址。   
port:端口号,整数,可选,省略时使用方案的默认端口,如http的默认端口为80。   
path:路径,由零或多个"/"符号隔开的字符串,一般用来表示主机上的一个目录或文件地址。   
query:查询,可选,用于给动态网页传递参数,可有多个参数,用"&"符号隔开,每个参数的名和值用"="符号隔开。   
fragment:信息片断,字符串,用于指定网络资源中的片断。即锚点.) 

1, window.location.href   整个URl字符串(在浏览器客户端中就是完整的地址栏)   
2,window.location.protocol    URL 的协议部分
3,window.location.host     URL 的主机部分
4,window.location.port      URL 的端口部分。如果默认80端口,那么返回值并不是默认的80而是空字符。
5,window.location.pathname   URL 的路径部分(就是文件地址)   
6,window.location.search   查询(参数)部分。
除了给动态语言赋值以外,我们同样可以给静态页面,并使用javascript来获得相信应的参数值
7,window.location.hash   锚点


js:

设置或获取对象指定的文件名或路径。
<script>
alert(window.location.pathname)
</script>

设置或获取整个 URL 为字符串。
<script>

alert(window.location.href);
</script>
设置或获取与 URL 关联的端口号码。
<script>
alert(window.location.port)
</script>

设置或获取 URL 的协议部分。
<script>
alert(window.location.protocol)
</script>

设置或获取 href 属性中在井号“#”后面的分段。
<script>
alert(window.location.hash)
</script>

设置或获取 location 或 URL 的 hostname 和 port 号码。
<script>
alert(window.location.host)
</script>

设置或获取 href 属性中跟在问号后面的部分。
<script>
alert(window.location.search)
</script>
0 0
原创粉丝点击