读书人

几种js获取url及参数, 并分析url的任意

发布时间: 2014-02-19 18:14:29 作者: rapoo

1.获取URL

获取url的方法,直接用location就可以了,window.location或window.loation.href也可以。

<script language="javascript">

var t1=location;

var t2=window.location;

var t3=window.location.href;

document.write("location="+t1+"<br />"+"window.location="+t2+"<br />"+"window.location.href="+t3);

</script>

2.分析URL

运用location获取的url的属性为object,要对其进行<a href="http://med7.cn/tag/%3Ca%20href=" http:="" med7.cn="" tag="" 字符"="" title="字符" style="text-decoration: none; outline: 0px; color: rgb(102, 102, 102);">字符串" title="字符串">字符串的分析操作,需要先将其转换为<a href="http://med7.cn/tag/%3Ca%20href=" http:="" med7.cn="" tag="" 字符"="" title="字符" style="text-decoration: none; outline: 0px; color: rgb(102, 102, 102);">字符串" title="字符串">字符串。

<script language="javascript" type="text/javascript">

var t1=location;

var t2=window.location;

var t3=window.location.href;

document.write("location类型为: "+typeof(t1)+"<br />"+"window.location类型为: "+typeof(t2)+"<br />"+"window.location.href类型为: "+typeof(t3));

</script>

用location来进行处理,首先将其转换为<a href="http://med7.cn/tag/%3Ca%20href=" http:="" med7.cn="" tag="" 字符"="" title="字符" style="text-decoration: none; outline: 0px; color: rgb(102, 102, 102);">字符串" title="字符串">字符串,用函数toString(),URL.toString();

<script language="javascript" type="text/javascript">

var t1=location;

var URL=t1.toString();

document.write("location类型为: "+typeof(t1)+"<br />"+"URL类型为: "+typeof(URL));

</script>

在URL中查找需要的内容,比如我我们需要查找id的值,根据页面的URL可以看出id=后面就是,进行如下查找。

<script language="javascript" type="text/javascript">

window.location.href="http://http://guokai2009.blog.hexun.com/default.xasp?id=70";

var t1=location;

var URL=t1.toString();

document.write("location类型为: "+typeof(t1)+"<br />"+"URL类型为: "+typeof(URL));

if(URL.indexOf("id")==-1){

alert("there is no id!");

}else{

var id=URL.split("id=");

alert(id[1]);

}

</script>

获取URL里的文件夹名:

<script language="javascript" type="text/javascript">

var folder=window.location.pathname;

var f=new Array();

f=folder.split("/");

alert("文件夹地址为:"+f[1]);

</script>

另附一些URL参数

完整的URL由这几个部分构成:

scheme://host:port/path?query#fragment

scheme = 通信协议 (常用的http,ftp,maito等)

host = 主机 (域名或IP)

port = 端口号

path = 路径

query = 查询

可选,用于给动态网页(如使用CGI、ISAPI、PHP/JSP/ASP/ASP.NET等技术制作的网页)传递参数,可有多个参数,用”&”符号隔开,每个参数的名和值用”=”符号隔开。

fragment = 信息片断

<a href="http://med7.cn/tag/%3Ca%20href=" http:="" med7.cn="" tag="" 字符"="" title="字符" style="text-decoration: none; outline: 0px; color: rgb(102, 102, 102);">字符串" title="字符串">字符串,用于指定网络资源中的片断。例如一个网页中有多个名词解释,可使用fragment直接定位到某一名词解释。(也称为锚点.)

对于这样一个URL

http://hexun.com.com:80/seo/?ver=1.0&id=6#imhere

我们可以用javascript获得其中的各个部分

1, window.location.href

整个URl<a href="http://med7.cn/tag/%3Ca%20href=" http:="" med7.cn="" tag="" 字符"="" title="字符" style="text-decoration: none; outline: 0px; color: rgb(102, 102, 102);">字符串" title="字符串">字符串(在浏览器中就是完整的<a href="http://med7.cn/tag/%3Ca%20href=" http:="" med7.cn="" tag="" 地址"="" title="地址" style="text-decoration: none; outline: 0px; color: rgb(102, 102, 102);">地址栏" title="地址栏">地址栏)

2,window.location.protocol

URL 的协议部分

本例<a href="http://med7.cn/tag/%3Ca%20href=" http:="" med7.cn="" tag="" 返回"="" title="返回" style="text-decoration: none; outline: 0px; color: rgb(102, 102, 102);">返回值" title="返回值">返回值:http:

3,window.location.host

URL 的主机部分

本例<a href="http://med7.cn/tag/%3Ca%20href=" http:="" med7.cn="" tag="" 返回"="" title="返回" style="text-decoration: none; outline: 0px; color: rgb(102, 102, 102);">返回值" title="返回值">返回值:hexun.com

4,window.location.port

URL 的端口部分

如果采用默认的80端口(update:即使添加了:80),那么<a href="http://med7.cn/tag/%3Ca%20href=" http:="" med7.cn="" tag="" 返回"="" title="返回" style="text-decoration: none; outline: 0px; color: rgb(102, 102, 102);">返回值" title="返回值">返回值并不是默认的80而是空字符

本例<a href="http://med7.cn/tag/%3Ca%20href=" http:="" med7.cn="" tag="" 返回"="" title="返回" style="text-decoration: none; outline: 0px; color: rgb(102, 102, 102);">返回值" title="返回值">返回值:”"

5,window.location.pathname

URL 的路径部分(就是文件地址)

本例<a href="http://med7.cn/tag/%3Ca%20href=" http:="" med7.cn="" tag="" 返回"="" title="返回" style="text-decoration: none; outline: 0px; color: rgb(102, 102, 102);">返回值" title="返回值">返回值:/seo/

6,window.location.search

查询(参数)部分

除了给动态语言赋值以外,我们同样可以给静态页面,并使用javascript来获得相信应的参数值

本例<a href="http://med7.cn/tag/%3Ca%20href=" http:="" med7.cn="" tag="" 返回"="" title="返回" style="text-decoration: none; outline: 0px; color: rgb(102, 102, 102);">返回值" title="返回值">返回值:?ver=1.0&id=6

7,window.location.hash

锚点

本例<a href="http://med7.cn/tag/%3Ca%20href=" http:="" med7.cn="" tag="" 返回"="" title="返回" style="text-decoration: none; outline: 0px; color: rgb(102, 102, 102);">返回值" title="返回值">返回值:#imhere

最后介绍一个郭恺刚刚使用的 这个能让你获取任何一部分

<script language=javascript>

<a href="http://med7.cn/tag/fu%3Ca%20href=" http:="" med7.cn="" tag="" nc"="" title="nc" style="text-decoration: none; outline: 0px; color: rgb(102, 102, 102);">nction" title="function">function <a href="http://med7.cn/tag/fi%3Ca%20href=" http:="" med7.cn="" tag="" len"="" title="len" style="text-decoration: none; outline: 0px; color: rgb(102, 102, 102);">lename" title="filename">filename() {

var a= document.location.href;

var n1=a.lastIndexOf('/')+1   

var n2=a.lastIndexOf('_d.html')

a=a.substring(n1,n2)

alert(a); 


}

<a href="http://med7.cn/tag/fi%3Ca%20href=" http:="" med7.cn="" tag="" len"="" title="len" style="text-decoration: none; outline: 0px; color: rgb(102, 102, 102);">lename" title="filename">filename()

</script>

将取出当前地址文件的名字,不带后缀

//a.lastIndexOf(\'/\') 从右向左取出第一个“/”的位置,也可以写成a.lastIndexOf(\'/\',0)第二个参数0,表示从左端0位置算起。第二个参数不写默认为0

//另外一个函数就是indexOf(\'/\',0),是从左向右查找。 第二个参数0,表示从左端0位置算起

--EOF--


读书人网 >JavaScript

热点推荐