Home > Web Front-end > JS Tutorial > jQuery gets address bar parameter plug-in (imitates C#)_jquery

jQuery gets address bar parameter plug-in (imitates C#)_jquery

WBOY
Release: 2016-05-16 18:17:35
Original
994 people have browsed it
复制代码 代码如下:

$.request = (function () {
var apiMap = {};
function request(queryStr) {
var api = {};
if (apiMap[queryStr]) {
return apiMap[queryStr];
}
api.queryString = (function () {
var urlParams = {};
var e,
d = function (s) { return decodeURIComponent(s.replace(/ /g, " ")); },
q = queryStr.substring(queryStr.indexOf('?') 1),
r = /([^&=] )=?([^&]*)/g;
while (e = r.exec(q))
urlParams[d(e[1])] = d(e[2]);

return urlParams;
})();
api.getUrl = function () {
var url = queryStr.substring(0, queryStr.indexOf('?') 1);
for (var p in api.queryString) {
url = p '=' api.queryString[p] "&";
}
if (url.lastIndexOf('&') == url.length - 1) {
return url.substring(0, url.lastIndexOf('&'));
}
return url;
}
apiMap[queryStr] = api;
return api;
}
$.extend(request, request(window.location.href));
return request;
})();

接下来简介绍一下插件的用法。
  我们可以通过 $.request.queryString["key"] 就能获取到地址栏上面key参数。
  大多数情况下我用这个插件来读取一些配置信息。
  我们有时候会在 html 标签中加入一些其他的非HTML属性来作为标志信息,比如我们有时候会写Demo
  这样子我们通过   
    var config = $.request($("#demo").attr("config")).queryString;
    var title = config.title;
  很方便就能读取a上面的配置信息,而且我们不需要给a 添加太多其他的非Html属性。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template