下面我们举例一个URL,然后获得它的各个组成部分:

http://i.cnblogs.com/EditPosts.aspx?opt=1

window.location.href (设置或获取整个 URL 为字符串)

var test = window.location.href;
alert(test);
// 返回:http://i.cnblogs.com/EditPosts.aspx?opt=1

window.location.protocol (设置或获取 URL 的协议部分)

var test = window.location.protocol;
alert(test);
//返回:http:

window.location.host (设置或获取 URL 的主机部分)

var test = window.location.host;
alert(test);
//返回:i.cnblogs.com

window.location.port (设置或获取与 URL 关联的端口号码)

var test = window.location.port;
alert(test);
//返回:空字符(如果采用默认的80端口 (update:即使添加了:80),那么返回值并不是默认的80而是空字符)

window.location.pathname (设置或获取与 URL 的路径部分(就是文件地址))

var test = window.location.pathname;
alert(test);
//返回:/EditPosts.aspx

window.location.search (设置或获取 href 属性中跟在问号后面的部分)

var test = window.location.search;
alert(test);
//返回:?opt=1
(PS:获得查询(参数)部分,除了给动态语言赋值以外,我们同样可以给静态页面,并使用javascript来获得相信应的参数值。)

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

var test = window.location.hash;
alert(test);
//返回:空字符(因为url中没有)

js获取url中的参数值*

正则法

function getQueryString(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
var r = window.location.search.substr(1).match(reg); if (r != null) {
return unescape(r[2]);
}
return null;
}
// 这样调用:
alert(GetQueryString("参数名1")); alert(GetQueryString("参数名2")); alert(GetQueryString("参数名3"));

split拆分法

function GetRequest() {
var url = location.search; //获取url中"?"符后的字串
var theRequest = new Object(); if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for(var i = 0; i < strs.length; i ++) {
theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
}
}
return theRequest;
}
var Request = new Object();
Request = GetRequest();<br>// var id=Request["id"];
// var 参数1,参数2,参数3,参数N;
// 参数1 = Request['参数1'];
// 参数2 = Request['参数2'];
// 参数3 = Request['参数3'];
// 参数N = Request['参数N'];

指定取

比如说一个url: http://i.cnblogs.com/?j=js, 我们想得到参数j的值,可以通过以下函数调用。

function GetQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
var context = ""; if (r != null)
context = r[2];
reg = null;
r = null;
return context == null || context == "" || context == "undefined" ? "" : context;
}
alert(GetQueryString("j"));

单个参数的获取方法

function GetRequest() {
var url = location.search; //获取url中"?"符后的字串
if (url.indexOf("?") != -1) {? //判断是否有参数
var str = url.substr(1); //从第一个字符开始 因为第0个是?号 获取所有除问号的所有符串
strs = str.split("=");? //用等号进行分隔 (因为知道只有一个参数
//所以直接用等号进分隔 如果有多个参数 要用&号分隔 再用等号进行分隔)
alert(strs[1]);???? //直接弹出第一个参数 (如果有多个参数 还要进行循环的)
}
}

转载自: https://www.jianshu.com/p/073f79c5e438

JS获取页面URL信息的更多相关文章

  1. js获取页面url的方法

    我们可以用javascript获得其中的各个部分 1, window.location.href 整个URl字符串(在浏览器中就是完整的地址栏) 本例返回值: http://ifisker.com/b ...

  2. js 获取当前URL信息

    document.location 这个对象包含了当前URL的信息 location.host 获取port号 location.hostname 设置或获取主机名称 location.href 设置 ...

  3. Javascrip获取页面URL信息

    使用Javascript可以方便获得页面的参数信息,常用的几种如下: 设置或获取对象指定的文件名或路径 window.location.pathname   设置或获取整个 URL 为字符串 wind ...

  4. js获取页面url中的各项值

    一. 通过window.location获取各项参数 1.获取页面完整的url url = window.location.href; 2.获取页面的域名 host = window.location ...

  5. js获取当前url信息

    window.location 属性 描述 hash 设置或获取 href 属性中在井号"#"后面的分段. host 设置或获取 location 或 URL 的 hostname ...

  6. js获取页面url

    设置或获取对象指定的文件名或路径. window.location.pathname例:http://localhost:8086/topic/index?topicId=361alert(windo ...

  7. 获取页面url信息

    方法: window.location.href = prefixURL+'webstatic/messageAnalysis/datadetail.html?id=' + num + "& ...

  8. JS获取页面传过来的值

    利用JS获取页面的传值,此方法只适应Get传值. 获取页面之间的传值,在后台我们很容易获取,那我们在前台只利用JS怎么写呢? 在看代码之前你需要了解的 ① 参考:W3C Location 对象 Loc ...

  9. JS获取页面数据执行Ajax请求

    下面这个例子展示了如何使用js获取页面中元素的值,并且将这些值作为参数执行Ajax请求. $("#submit-task").bind("click", fun ...

随机推荐

  1. review41

    套接字是基于TCP协议的网络通信. 基于UDP

  2. Node的异步I/O

    node是单线程非阻塞异步I/O的模式. 阻塞I/O:完成整个数据获取的过程: 非阻塞I/O:不带数据,直接立即返回,要获取数据,还需通过文件描述符再次读取. node完成整个异步I/O的有事件循环. ...

  3. SpringBoot使用devtools导致的类型转换异常

    遇到的问题:SpringBoot项目中的热部署引发的血的教训,报错代码位置: XStream xStream1 = new XStream(); xStream1.autodetectAnnotati ...

  4. 09-THREE.JS 物体缩放,坐标,旋转,位移,是否可见

    <!DOCTYPE html> <html> <head> <title></title> <script src="htt ...

  5. Mybatis 接口方式对数据的增删改查 一对一关联查询

    数据库中有两个表 student 和studentInfo student表中的字段和数据 studentInfo表中的字段 ok数据库说完了,开始建立一个项目,对数据库中的数据进行操作吧 新建jav ...

  6. php 5.6以上可以采用new PDD连接数据库的方法。

    <?php// @mysqli_connect($db=localhost,// $_cont['root'],// $_cont['root'],// $_cont['demo'],// $_ ...

  7. 把数据库里面的数据导出来csv

    function export_csv($filename, $data, $head = '') { $string = $head; foreach ($data as $key => $v ...

  8. PhotoShop使用指南(1)——动态图gif的制作

    第一步:菜单栏 > 窗口 > 工作区 > 动感 第二步:时间轴 > 设置延迟时间 第三步:时间轴 > 设置循环次数 第四步:存储为Web所用格式 Ctrl+Shift+A ...

  9. linux shell 学习笔记--变量声明与赋值,循环

    Bash 变量是不分类型的 ------------------------ 不像其他程序语言一样,Bash 并不对变量区分"类型".本质上,Bash 变量都是字符串. 但是依赖于 ...

  10. 使用kaptcha验证码组件操作演示

    1.创建一个Maven项目 2.在pom.xml中引入相关依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...