下面我们举例一个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. poi读取Excel内容数据

    public static void main(String[] args) { try{ //获取文件输入流 FileInputStream fileIn = new FileInputStream ...

  2. R语言常用语法总结

    ## 1. 数据输入 ##a$b # 数据框中的变量a = 15 # 赋值a <- 15 # 赋值a = c(1,2,3,4,5) # 数组(向量)b = a[1] # 数组下标,从1开始b = ...

  3. Centos7+httpd+fastcgi安装提示错误

    搭建的环境: centos7 Apache/2.4.6 fastcgi2.4.6 rails4 在安装fastcgi的时候遇到了问题: 问题: ...... In file included from ...

  4. 使用junit单元测试

    使用junit单元测试 一.方法 二.说明 使用这个测试函数或者调试错误非常方便 三.代码实例 后面补

  5. substr 方法

    substr 方法 返回一个从指定位置开始,并具有指定长度的子字符串. 参数 start 必选.所需的子字符串的起始位置.字符串中第一个字符的索引为 0. length 可选项.返回的子字符串中包含的 ...

  6. 分布式_理论_05_ 一致性算法 Paxos

    一.前言 二.参考资料 1.分布式理论(五)—— 一致性算法 Paxos 2.分布式理论(五) - 一致性算法Paxos

  7. vs2010 oraclelient 引用问题

    不能正常引用 oracleclent :错误信息如下 ================================================================= 排除1. 当前 ...

  8. hibernate ORM related

    一.单向关联(unidirectional associations): 1.1.1 Many-to-one Employee.hbm.xml <class name="Employe ...

  9. 【暂时解决】win10下安装VS2017 15.3版本 提示 未能安装包“Microsoft.NET.4.6.FullRedist.NonThreshold.Resources,version=4.6.81.9,language=zh-CN”。

    win10下安装VS2017 15.3版本的时候,出现以上错误日志提示,请问如何解决的哇? 这个问题,开始我以为是我的安装包所在的路径问题引起的,但是我将安装包移动到了磁盘根目录进行安装,依然出现这个 ...

  10. New Concept English three (48)

    23w/m 76errors In this much-travelled world, there are still thousands of places which are inaccessi ...