码迷,mamicode.com
首页 > Web开发 > 详细

取得get参数 从url

时间:2018-06-11 18:57:17      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:view   3.2   preview   title   window   keditor   params   arch   cat   

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

标签:view   3.2   preview   title   window   keditor   params   arch   cat   

原文地址:https://www.cnblogs.com/mingzhanghui/p/9168297.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!