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 ...
随机推荐
- Java for LeetCode 104 Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- ES6中的class 与prototype
一.定义构造函数 在以前的js中,生成一个对象实例,需要先定义构造函数,然后通过prototype 的方式来添加方法,在生成实例: function Person(){ this.name = &qu ...
- python把源代码打包成.exe文件
1.在windows命令行把当前文件夹用cd命令切换到源代码所在文件夹. 2.输入命令:pyinstaller -w -F main.py
- 日期时间选择器bootstrap-datetimepicker表单组件
Bootstrap受到很多人的喜欢,它不仅拥有一套完整漂亮的UI,而且爱好者们为其开发扩展了很多有用的插件和主题!让其拥有无限可能! 今天为开发者推荐一款强大,易用的时间日历插件——bootstrap ...
- (转)C++经典面试题(最全,面中率最高)
1.new.delete.malloc.free关系 delete会调用对象的析构函数,和new对应free只会释放内存,new调用构造函数.malloc与free是C++/C语言的标准库函数,new ...
- dead reckoning variation
Targeting A target is a structure of information that describes the state, and change of state, of a ...
- 华为机试 可怕的N阶乘
题目标题: 计算阶乘n!是一件可怕的事情,因为当n并不是很大时,n!将是一个很大的值.例如13! = 6227020800,已经超过了我们常用的unsigned int类型的取值范围.请设计一个程序, ...
- codeforces 651E E. Table Compression(贪心+并查集)
题目链接: E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- composer镜像安装laravel
博主最近在学习Laravel的框架的相关知识,对于Laravel的许多新特性,大家最好还是去查看官网文档最好,Laravel的文档非常完善,中文英文的都有,可以很好的解决你的困惑. 但是我们会发现学习 ...
- osx快捷键表示图