
传送门 在 WEB 开发中,时常会用到 javascript 来获取当前页面的 url 网址信息,在这里是我的一些获取 url 信息的小总结。
下面我们举例一个 URL,然后获得它的各个组成部分:http://i.cnblogs.com/EditPosts.aspx?opt=1
1 2 | var test = window.location.href; alert(test); |
|---|
返回:http://i.cnblogs.com/EditPosts.aspx?opt=1
1 2 | var test = window.location.protocol; alert(test); |
|---|
返回:http:
1 2 | var test = window.location.host; alert(test); |
|---|
返回:i.cnblogs.com
1 2 | var test = window.location.port; alert(test); |
|---|
返回:空字符(如果采用默认的 80 端口 (update: 即使添加了:80),那么返回值并不是默认的 80 而是空字符)
1 2 | var test = window.location.pathname; alert(test); |
|---|
返回:/EditPosts.aspx
1 2 | var test = window.location.search; alert(test); |
|---|
返回:?opt=1
PS:获得查询(参数)部分,除了给动态语言赋值以外,我们同样可以给静态页面,并使用 javascript 来获得相信应的参数值。
1 2 | var test = window.location.hash; alert(test); |
|---|
返回:空字符(因为 url 中没有)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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")); |
|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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 的值,可以通过以下函数调用。
1 2 3 4 5 6 7 8 9 10 11 | 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")); |
|---|
由于之前把 blog 和网站主页分开在两个仓库所以要想在 blog 中 menu 里跳转到站外链接就要做一些处理。以前一直百度不到。 其实想法早就有了,只要在 blog 首页或者网站首页检测到https://lruihao.cn/home这个链接,或者检测到 home 字段就自动跳转。想法很简单。可是对 js 真的一点都不了解,以前百度也找不到实际的效果案例。所以还是自己写吧!附上蹩脚代码。
1 2 3 4 5 | var path = window.location.href; //alert(path); if (path=='https://lruihao.cn/home/') { window.location.replace("https://www.lruihao.cn"); } |
|---|
或者
1 2 3 4 5 | var path = window.location.pathname; //alert(path); if (path=='/home/') { window.location.replace("https://www.lruihao.cn"); } |
|---|
http 强制重定向 https
1 2 3 4 5 6 7 8 | <script> var targetProtocol = "https:"; var host = "lruihao.cn"; //域名判断,因为 localhost 仅支持 http if (window.location.host == host && window.location.protocol != targetProtocol){ window.location.href = targetProtocol + window.location.href.substring(window.location.protocol.length); } </script> |
|---|