取得get参数 从url

Posted mingzhanghui

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了取得get参数 从url相关的知识,希望对你有一定的参考价值。

1. getUrlParam.js

define(function() { // url参数
    var data, index;
    (function init() {
        data = [];
        index = {};
        var u = window.location.search.substr(1);
        if (u != ‘‘) {
            var params = decodeURIComponent(u).split(‘&‘);
            for (var i = 0, len = params.length; i < len; i++) {
                if (params[i] != ‘‘) {
                    var p = params[i].split("=");
                    if (p.length == 1 || (p.length == 2 && p[1] == ‘‘)) {// p | p=
                        data.push([‘‘]);
                        index[p[0]] = data.length - 1;
                    } else if (typeof(p[0]) == ‘undefined‘ || p[0] == ‘‘) { // =c | =
                        data[0] = [p[1]];
                    } else if (typeof(index[p[0]]) == ‘undefined‘) { // c=aaa
                        data.push([p[1]]);
                        index[p[0]] = data.length - 1;
                    } else {// c=aaa
                        data[index[p[0]]].push(p[1]);
                    }
                }
            }
        }
    })();
    return {
        // 获得参数,类似request.getParameter()
        param : function(o) { // o: 参数名或者参数次序
            try {
                return (typeof(o) == ‘number‘ ? data[o][0] : data[index[o]][0]);
            } catch (e) {
            }
        },
        //获得参数组, 类似request.getParameterValues()
        paramValues : function(o) { //  o: 参数名或者参数次序
            try {
                return (typeof(o) == ‘number‘ ? data[o] : data[index[o]]);
            } catch (e) {}
        },
        //是否含有paramName参数
        hasParam : function(paramName) {
            return typeof(paramName) == ‘string‘ ? typeof(index[paramName]) != ‘undefined‘ : false;
        },
        // 获得参数Map ,类似request.getParameterMap()
        paramMap : function() {
            var map = {};
            try {
                for (var p in index) {  map[p] = data[index[p]];  }
            } catch (e) {}
            return map;
        }
    }
});

2. require.js 配置

// require.js 依赖的js库路径配置
require.config({
    paths : {
        // "jquery-1.12" : "../assets/jqui/external/jquery/jquery",
        "jquery-ui": "../assets/jqui/jquery-ui",
        // "jquery": "../assets/bootstrap/js/jquery-3.3.1.min",
        "jquery": "../assets/ckeditor/js/jquery-3.2.1",
        "ckeditor-core": "../assets/ckeditor/ckeditor",
        ‘ckeditor-jquery‘: "../assets/ckeditor/adapters/jquery",
        "jquery-cookie": "../assets/js/jquery.cookie",
        "bootstrap": "../assets/bootstrap/js/bootstrap",
        "alert": "js/lib/alert",
        "supersized": "../assets/js/login/supersized.3.2.7",
        "url-param": "js/util/getUrlParam",
        ‘image-preview‘: ‘js/util/preview‘
    },
    shim: {
        ‘ckeditor-jquery‘:{
            deps:[‘jquery‘,‘ckeditor-core‘]
        },
        ‘jquery-cookie‘: {
            deps: [‘jquery‘]
        },
        ‘bootstrap‘: {
            deps: [‘jquery‘]
        },
        ‘jquery-ui‘:{
            deps: [‘jquery‘]
        }
    }
});

  3. 使用

require([‘url-param‘], function(URLParam) {
         var data = {
                id: URLParam.param("id"),
                schoolName: URLParam.param(‘schoolName‘),
                columnName: URLParam.param(‘columnName‘),
                title: URLParam.param("title")
         };
})

  

以上是关于取得get参数 从url的主要内容,如果未能解决你的问题,请参考以下文章

Get和Post区别?

jQuery获取URL的GET参数值

httpservletrequest的对象getParam();是从哪取得的参数?

httpservletrequest的对象getParam();是从哪取得的参数?

django url路由参数错误

原生JavaScript获得URL中GET参数值