js获取当前地址栏的域名、Url、相对路径和参数以及指定参数
以下代码整理于网络
1、设置或获取对象指定的文件名或路径。
window.location.pathname
例:http://localhost:8086/topic/index?topicId=361
alert(window.location.pathname); 则输出:/topic/index
2、设置或获取整个 URL 为字符串。
window.location.href
例:http://localhost:8086/topic/index?topicId=361
alert(window.location.href); 则输出:http://localhost:8086/topic/index?topicId=361
3、设置或获取与 URL 关联的端口号码。
window.location.port
例:http://localhost:8086/topic/index?topicId=361
alert(window.location.port); 则输出:8086
4、设置或获取 URL 的协议部分。
window.location.protocol
例:http://localhost:8086/topic/index?topicId=361
alert(window.location.protocol); 则输出:http:
5、设置或获取 href 属性中在井号“#”后面的分段。
window.location.hash
6、设置或获取 location 或 URL 的 hostname 和 port 号码。
window.location.host
例:http://localhost:8086/topic/index?topicId=361
alert(window.location.host); 则输出:http:localhost:8086
7、设置或获取 href 属性中跟在问号后面的部分。
window.location.search
例:http://localhost:8086/topic/index?topicId=361
alert(window.location.search); 则输出:?topicId=361
window.location
属性 描述
href 设置或获取整个 URL 为字符串。
host 设置或获取 location 或 URL 的 hostname 和 port 号码。
protocol 设置或获取 URL 的协议部分。
port 设置或获取与 URL 关联的端口号码。
hostname 设置或获取 location 或 URL 的主机名称部分。
pathname 设置或获取对象指定的文件名或路径。
search 设置或获取 href 属性中跟在问号后面的部分。
hash 设置或获取 href 属性中在井号“#”后面的分段。
一、js获取当前域名有2种方法
1、方法一 var domain = document.domain; 2、方法二 var domain = window.location.host; 3、注意问题 由于获取到的当前域名不包括 http://,所以把获取到的域名赋给 a 标签的 href 时,别忘了加上 http://,否则单击链接时导航会出错。
二、获取当前Url的4种方法
var url = window.location.href; var url = self.location.href; var url = document.URL; var url = document.location; ie 地址栏显示的是什么,获取到的 url 就是什么。
三、获取当前相对路径的方法
首先获取 Url,然后把 Url 通过 // 截成两部分,再从后一部分中截取相对路径。如果截取到的相对路径中有参数,则把参数去掉。 function GetUrlRelativePath()
{
var url = document.location.toString();
var arrUrl = url.split("//"); var start = arrUrl[1].indexOf("/");
var relUrl = arrUrl[1].substring(start);//stop省略,截取从start开始到结尾的所有字符 if(relUrl.indexOf("?") != -1){
relUrl = relUrl.split("?")[0];
}
return relUrl;
} 调用方法:GetUrlRelativePath(); 举例:假如当前 Url 是 http// www. liangshunet. com/pub/item.aspx?t=osw7,则截取到的相对路径为:/pub/item.aspx。
四、获取当前Url参数的方法
1、获取Url参数部分 function GetUrlPara()
{
var url = document.location.toString();
var arrUrl = url.split("?"); var para = arrUrl[1];
return para;
} 调用方法:GetUrlPara() 举例:假如当前 Url 是 http// www. liangshunet. com/pub/item.aspx?t=osw7,则截取到的参数部分为:t=osw7。
五、获取指定Url参数的方法
//paraName 等找参数的名称
function GetUrlParam(paraName) {
var url = document.location.toString();
var arrObj = url.split("?"); if (arrObj.length > 1) {
var arrPara = arrObj[1].split("&");
var arr; for (var i = 0; i < arrPara.length; i++) {
arr = arrPara[i].split("="); if (arr != null && arr[0] == paraName) {
return arr[1];
}
}
return "";
}
else {
return "";
}
} 调用方法:GetUrlParam("id"); 举例说明: 假如当网页的网址有这样的参数 test.htm?id=896&s=q&p=5,则调用 GetUrlParam("p"),返回 5。
js获取当前地址栏的域名、Url、相对路径和参数以及指定参数的更多相关文章
- js获取当前域名、Url、相对路径和参数以及指定参数
一.js获取当前域名有2种方法 1.方法一 var domain = document.domain; 2.方法二 var domain = window.location.host; 3.注意问题 ...
- 使用js获取浏览器地址栏里的参数
用JS获取地址栏参数的方法(超级简单) 方法一:采用正则表达式获取地址栏参数:( 强烈推荐,既实用又方便!) function GetQueryString(name) { var reg = new ...
- JS获取浏览器地址栏的多个参数值的任意值
getParamValue("id"); //http://localhost:2426/TransactionNotes.aspx?id=100 //返回值是100: // 根据 ...
- js获取当前页面的网址域名地址
1.获取当前完整网址thisURL = document.URL;thisHREF = document.location.href;thisSLoc = self.location.href;thi ...
- 使用JS获取上一页的url地址
一般来说每个页面上面都有一个返回按钮,用来返回上一页,代码如下: <a href="javascript:history.go(-1)" class="jsBack ...
- js获取或设置当前窗口url参数
直接上代码 // 获取当前窗口url中param参数的值 function get_param(param){ var query = location.search.substring(1).spl ...
- JS获取浏览器地址栏的多参数值的任意值
常用的几个方法就不讲了,这里我用的是两个方法组 使用方法是: getParamValue("id"); http://localhost:2426/TransactionNotes ...
- js获取来源和当前域名
参考:http://www.cnblogs.com/zuosong160522/p/5755615.html http://www.oicqzone.com/pc/2014113020362.html
- js获取浏览器地址栏传递的参数
function getQueryString(key){ var href=window.location.href; var reg = new RegExp(key +"=([^&am ...
随机推荐
- [2018-05-27]配置VSTS认证方式使用Personal Access Token
本文介绍下如何配置VSTS(visual studio team service,其实就是微软SaaS版的TFS)通过Personal Access Token访问其下的Git代码库. 问题 使用gi ...
- iOS实时监控网络状态的改变
在网络应用中,有的时候需要对用户设备的网络状态进行实时监控,有两个目的: (1)让用户了解自己的网络状态,防止一些误会(比如怪应用无能) (2)根据用户的网络状态进行智能处理,节省用户流量,提高用户体 ...
- 延时加载 lazyload使用技巧
html <img class="lazy" src="images/src_unit.png" data-src="images/index/ ...
- 【Codeforces】Round #460 E - Congruence Equation 中国剩余定理+数论
题意 求满足$na^n\equiv b \pmod p$的$n$的个数 因为$n \mod p $循环节为$p$,$a^n\mod p$循环节为$p-1$,所以$na^n \mod p$循环 ...
- codeforces 569B B. Inventory(水题)
题目链接: B. Inventory time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- jQuery精华
第一章:入门 选择元素: $() css() $("li").css():可以省略原生的循环操作 $ == jQuery jQuery方法函数化: click() html() J ...
- MySQL_杭州拱墅区、西湖区近9-11月销售过的产品_20161125
需求:杭州拱墅区.西湖区近9-11月销售过的产品 这个SQL如果对数据表熟悉的话可以一步到位,为了看的更清楚,时间间隔在3个月产品数量也不是很多,采取先找明细再进行汇总. 一.第一种写法 分步骤的写S ...
- CF 1042A Benches——二分答案(水题)
题目:http://codeforces.com/problemset/problem/1042/A #include<iostream> #include<cstdio> # ...
- bzoj 1004 Cards & poj 2409 Let it Bead —— 置换群
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1004 关于置换群:https://www.cnblogs.com/nietzsche-oie ...
- Set connectionId threw an exception.
今天调试一个WPF程序时,出现一个问题. 程序运行后抛出异常, "Set connectionId threw an exception. XXXXXXXXXX",原因是依赖的一个 ...