下面我们举例一个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. 设置浏览器地址栏URL前面显示的图标

    其实很简单,你只做个ico图标,命名为favicon.ico,把它传到你的页面下面. 并在相应的页面里加上代码  在页面<heah></heah>之间加, <link r ...

  2. npm、模块暴露,小知识点区别

    --save-dev与--save的区别 npm install xxx --save-dev 是指将包信息添加到 package.json 里的 devDependencies节点,表示开发时依赖的 ...

  3. css3——transition属性和opacity属性

    [transition-duration] 是一个css3属性,规定完成过度效果需要花费的时间(一秒或毫秒计).语法:transition-duration: time;time :    规定完成过 ...

  4. poj2289二分图多重匹配

    题意:给你一张二分图,求右边点到汇点的最小容量(保证流量为n)是多少 题解:二分答案,每次重新建边跑最大流,看是不是为n就好了 #include<map> #include<set& ...

  5. C# 6.0 (VS2015 CTP6)

    /* C# 6.0 demo https://github.com/dotnet/roslyn/wiki/Languages-features-in-C%23-6-and-VB-14 */ using ...

  6. uva 103 经典DAG变形

    https://vjudge.net/problem/UVA-103 也是一个经典的DAG模型,因为书上的翻译和原文不照导致WA两发= = 对于同一维度的两个箱子A,B,A可以嵌套在B中的一个充分条件 ...

  7. 分布式事务_01_2PC框架raincat快速体验

    一.前言 关于2PC的理论知识请见:分布式_理论_03_2PC 这一节我们来看下github上一个优秀的2PC分布式事务开源框架的快速体验. 二.源码 源码请见: https://github.com ...

  8. LeetCode OJ:Binary Search Tree Iterator(二叉搜索树迭代器)

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  9. BestCoder Round #18(hdu5105)Math Problem(高中数学)

    最大值无非就是在两个端点或极值点处取得. 我注意讨论了a=0和b=0,却忽略了极值点可能不在L到R的范围内这一问题.被Hack了. #include<iostream> #include& ...

  10. ICE 的回调

    使用分布式计算中间件ICE到现在已经有一年多了,在这一年里里面对ICE的理解.应用比较熟悉. 使用ICE写分布式软件,确实是很方便:ICE比较稳定.可靠,调用返回速度低延迟,使用简单,学习曲线不是很陡 ...