惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
U
Unit 42
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
G
Google Developers Blog
Recent Announcements
Recent Announcements
B
Blog RSS Feed
罗磊的独立博客
博客园 - Franky
J
Java Code Geeks
S
SegmentFault 最新的问题
D
DataBreaches.Net
C
Check Point Blog
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
有赞技术团队
有赞技术团队
腾讯CDC
博客园_首页
美团技术团队
V
Visual Studio Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
GbyAI
GbyAI
The Cloudflare Blog
aimingoo的专栏
aimingoo的专栏

博客园 - leonardleonard

sql去重 分布式云存储系统 Riak CS 开源 转-WCF详解文档最新 .net 基础 HTTP 状态代码 大型网站架构不得不考虑的10个问题 怎么让蜘蛛喜欢你的网站 【Helper】泛型类和集合类的方法 靠垃圾站已经不能继续混下去了 初学做网站的草根站长必读 怎么样让网站有一个好听的中文名称 网站迅速产生外链的方法秘笈 想做成功网站需要注意的几个问题 一些JSON相关的函数 - leonardleonard - 博客园 Asp.NET 的Session实现原理 转自21kaiyun.com jquery技巧总结 JavaScript函数 this调用规则 只需九步让你新站成为高权重网站 影响Lucene索引速度原因以及提高索引速度技巧
21世纪开运网www.21kaiyun.com用javascript替换URL中的参数值...
leonardleonard · 2010-07-29 · via 博客园 - leonardleonard

今天遇到一个需要用javascript将url中的某些参数替换的需求:

    var a =  document.createElement('a');

        protocol: a.protocol.replace(':',''),

                seg = a.search.replace(/^\?/,'').split('&'),

                len = seg.length, i = 0, s;

                if (!seg[i]) { continue; }

        file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],

        hash: a.hash.replace('#',''),

        path: a.pathname.replace(/^([^\/])/,'/$1'),

        relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],

        segments: a.pathname.replace(/^\//,'').split('/')

用法:

myURL.file;     // = 'index.html'

myURL.hash;     // = 'top'

myURL.host;     // = 'abc.com'

myURL.query;    // = '?id=255&m=hello'

myURL.params;   // = Object = { id: 255, m: hello }

myURL.path;     // = '/dir/index.html'

myURL.segments; // = Array = ['dir', 'index.html']

myURL.port;     // = '8080'

myURL.protocol; // = 'http'

取得URL的参数,以对象形式返回!

var getParam = function(path){

    var result = {},param = /([^?=&]+)=([^&]+)/ig,match;

    while((match = param.exec(path)) != null){

        result[match[1]] = match[2];

Object.keys = Object.keys || function(obj){

        if(obj.hasOwnProperty(i)){

alert(Object.keys(r))//ct,tn,sc,pn,rn,lm,rs4,rs3,word,frs

posted @ 2010-07-29 16:22  leonardleonard  阅读(878)  评论()    收藏  举报