javascript获取当前url
在WEB开发中,许多开发者都比较喜欢使用javascript来获取当前url网址,本文就此为大家总结一下比较常用获取URL的javascript实现代码,以下示例是前面为相应实现方法,后面是获取URL的效果,下面以例子讲解:
输入的网址是(没有框架):http://localhost:81/Test/1.htm?Did=123
<br>以下为输出:
<br>
<SCRIPT>
//获取Url传过来的值
function Request(name)
{
new RegExp("(^|&)"+name+"=([^&]*)").exec(window.location.search.substr(1));
return RegExp.$2
}
注意:RegExp 是javascript中的一个内置对象。为正则表达式。
RegExp.$1是RegExp的一个属性,指的是与正则表达式匹配的第一个 子匹配(以括号为标志)字符串,以此类推,RegExp.$2,RegExp.$3,..RegExp.$99总共可以有99个匹配
给你看了例子就知道了
var r= /^(\d{4})-(\d{1,2})-(\d{1,2})$/; //正则表达式 匹配出生日期(简单匹配)
r.exec('1985-10-15');
s1=RegExp.$1;
s2=RegExp.$2;
s3=RegExp.$3;
alert(s1+" "+s2+" "+s3)//结果为1985 10 15
thisURL = document.URL; // http://localhost:81/Test/1.htm?Did=123
thisHREF = document.location.href; // http://localhost:81/Test/1.htm?Did=123
thisSLoc = self.location.href; // http://localhost:81/Test/1.htm?Did=123
thisDLoc = document.location; // http://localhost:81/Test/1.htm?Did=123
thisTLoc = top.location.href; // http://localhost:81/Test/1.htm?Did=123
thisPLoc = parent.document.location;// http://localhost:81/Test/1.htm?Did=123
thisTHost = top.location.hostname; // localhost
thisHost = location.hostname; // localhost
thisU1 = window.location.protocol; // http:
thisU2 = window.location.host; // localhost:81
thisU3 = window.location.pathname; // /Test/1.htm
document.writeln( thisURL + "<br />");
document.writeln( thisHREF + "<br />");
document.writeln( thisSLoc + "<br />");
document.writeln( thisDLoc + "<br />");
document.writeln( thisTLoc + "<br />");
document.writeln( thisPLoc + "<br />");
document.writeln( thisTHost + "<br />");
document.writeln( thisHost + "<br />");
document.writeln( thisU1 + "<br />");
document.writeln( thisU2 + "<br />");
document.writeln( thisU3 + "<br />");
document.writeln( "Did="+Request("Did") );// Did=123
</SCRIPT>
javascript获取当前url的更多相关文章
- Javascript 获取链接(url)参数的方法
有时我们需要在客户端获取链接参数,一个常见的方法是将链接当做字符串,按照链接的格式分解,然后获取对应的参数值.本文给出的就是这个流程的具体实现方法. 当然,我们也可以用正则直接匹配,文章中也给出了一个 ...
- javascript获取当前url中的參数
javascript获取当前页面url中的參数能够使用location的search方法,获取到的是url中?后面的部分,比如http:localhost:8080/Manager/index.jsp ...
- JavaScript获取当前url路径
1.假设当前页完整地址是:http://localhost:61768/Home/Index?id=2 //获取当前窗口的Url var url = window.location.href; //结 ...
- JavaScript获取当前url根目录(路径)
jsp: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8&q ...
- javascript 获取当前 URL 参数的两种方法
window.location.host; //返回url 的主机部分,例如:www.xxx.com window.location.hostname; //返回www.xxx.com window. ...
- JS实现获取当前URL和来源URL的方法
通用模式: Javascript 正常取来源网页的URL只要用: index.html: <!DOCTYPE html> <html lang="zh-cn"&g ...
- Javascript/jQuery 获取地址栏URL参数的方法
1.jquery获取url很简单,代码如下 window.location.href; 2.javascript获取url参数 function getUrlParam(name) { var reg ...
- 多浏览器兼容用javascript获取url参数的方法比较推荐的一种
多浏览器兼容用javascript获取url参数的方法比较推荐的一种 <script language = javascript> function request(paras){ var ...
- javascript获取url参数的方法
发布:thatboy 来源:Net [大 中 小] 本文介绍下,在javascript中取得url中某一个参数的方法,这里分享一个小例子,供大家学习参考下.本文转自:http://www. ...
随机推荐
- Codeforces Round #364 (Div. 2)
这场是午夜场,发现学长们都睡了,改主意不打了,第二天起来打的virtual contest. A题 http://codeforces.com/problemset/problem/701/A 巨水无 ...
- Page-encoding specified in XML prolog (UTF-8) is different from that specified in page directive (utf-8)
org.apache.jasper.JasperException:xxx.jsp(1,1) Page-encoding specified in XML prolog (UTF-8) is diff ...
- Spring Autowired 注入失败总是Null
报错:NullPointerException 分析:错误原因是注入失败? <context:annotation-config/> <context:component-scan ...
- Linux crontab 命令格式与详细例子
基本格式 :* * * * * command分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用*或者 */1表示第2列表示小时1-23(0表示0点)第3列表示日期1-31第4列表示月份1-1 ...
- Web用户的身份验证及WebApi权限验证流程的设计和实现
5. WebApi 服务端代码示例 5.1 控制器基类ApiControllerBase [csharp] view plaincopy /// /// Controller的基类,用于实现适合业 ...
- 推荐Python Web开发测试驱动方法
http://www.cnblogs.com/dkblog/archive/2013/06/14/3135914.html推荐 本人买的时候,京东打8.5折,现在降价啦,本书涵盖啦Django.Sel ...
- SQl中的char,varcher,nvarchar,txt
CHAR char是定长的,在字段建立时,空间就固定了,不管是否插入值(NULL也包含在内),都占用字符的空间.例如: char(8),输入字符小于8,后面补空值.输入字符大于8时,会截取.CH ...
- HTML5的全新语义化元素
1.<section> <section>元素用来定义文档或应用程序中的区域(或节).例如,可以用它组织你的个人信息,一个<section>用于联系信息,另一个用于 ...
- 返回数据方法DeaCacheCommand,由CRL自动实现
越来越多的人学起了前端,或许部分的初衷仅是它简单易上手以及好找工作,毕竟几年前只会个html和css就能有工作,悄悄告诉泥萌,这也是博主一年前的初衷 还好numpy, scikit-learn都提供了 ...
- PHP 使用 password_hash() 给密码加密
PHP >= 5.5 时,可以使用 password_hash() 和 password_verify() 来对用户的密码进行加密和验证,例如在用户注册(加密存储)和登陆(验证): <?p ...