在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的更多相关文章

  1. Javascript 获取链接(url)参数的方法

    有时我们需要在客户端获取链接参数,一个常见的方法是将链接当做字符串,按照链接的格式分解,然后获取对应的参数值.本文给出的就是这个流程的具体实现方法. 当然,我们也可以用正则直接匹配,文章中也给出了一个 ...

  2. javascript获取当前url中的參数

    javascript获取当前页面url中的參数能够使用location的search方法,获取到的是url中?后面的部分,比如http:localhost:8080/Manager/index.jsp ...

  3. JavaScript获取当前url路径

    1.假设当前页完整地址是:http://localhost:61768/Home/Index?id=2 //获取当前窗口的Url var url = window.location.href; //结 ...

  4. JavaScript获取当前url根目录(路径)

    jsp: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8&q ...

  5. javascript 获取当前 URL 参数的两种方法

    window.location.host; //返回url 的主机部分,例如:www.xxx.com window.location.hostname; //返回www.xxx.com window. ...

  6. JS实现获取当前URL和来源URL的方法

    通用模式: Javascript 正常取来源网页的URL只要用: index.html: <!DOCTYPE html> <html lang="zh-cn"&g ...

  7. Javascript/jQuery 获取地址栏URL参数的方法

    1.jquery获取url很简单,代码如下 window.location.href; 2.javascript获取url参数 function getUrlParam(name) { var reg ...

  8. 多浏览器兼容用javascript获取url参数的方法比较推荐的一种

    多浏览器兼容用javascript获取url参数的方法比较推荐的一种 <script language = javascript> function request(paras){ var ...

  9. javascript获取url参数的方法

    发布:thatboy   来源:Net     [大 中 小] 本文介绍下,在javascript中取得url中某一个参数的方法,这里分享一个小例子,供大家学习参考下.本文转自:http://www. ...

随机推荐

  1. js实现复制到剪贴板功能,兼容所有浏览器

    http://www.cnblogs.com/PeunZhang/p/3324727.html https://github.com/zeroclipboard/ZeroClipboard 复制链接到 ...

  2. Leetcode Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  3. jQuery学习笔记--JqGrid相关操作 方法列表(上)

    1.获得当前列表行数:$("#gridid").getGridParam("reccount"); 2.获取选中行数据(json):$("#gridi ...

  4. ArrayList实现删除重复元素(元素不是对象类型的情况)

    package 集合; import java.util.ArrayList;import java.util.Iterator; /* * 去除ArrayList里面的重复元素 *  * */pub ...

  5. this用法(ryf)

    this是javascript语言的一个关键字 它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用,比如: function test(){ this.x = 1; } 随着函数使用场合的不 ...

  6. hud 5876 2016 ACM/ICPC Asia Regional Dalian Online

    题意:给一个图 给定一个点s 求补图中s点到达各个点的最短路 思路:从s点开始bfs 在图中与s点有连接的都是在补图中不能直接到达的点 反之在补图中都是可以直接到达的点 由此bfs ((( 诡异的写法 ...

  7. java 正则表达式

    1.首先是说明一些容易混淆的符号 \w    Matches any word character. \W    Matches any non-word character. 如果是在java中的话 ...

  8. java面向对象_抽象类和接口

    一.抽象类 1.抽象方法:由abstract修饰.只有定义没有方法体.用一个分号结尾. 2.抽象类: 1)包含抽象方法的类必须是抽象类 2)由abstract修饰 3)不能被实例化 4)抽象类如果不被 ...

  9. C语言中定义全局变量

    (1)在C语言的头文件中定义变量出现的问题 最好不要傻嘻嘻的在头文件里定义什么东西.比如全局变量: /*xx头文件*/ #ifndef  _XX_头文件.H #define  _XX_头文件.H in ...

  10. java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)可能出现的原因

    可能是因为你的服务器http连接过多,导致端口被占用,无法释放