zoukankan      html  css  js  c++  java
  • 前端拼接跳转链接带参数

    function getRequest() {
        let search = window.location.search;
        search = decodeURI(search);
    
        let map = new Map();
        if (search.indexOf('?') != -1) {
    
            let strs = search.substr(1).split('&');
            for (let str of strs) {
                let key = decodeURIComponent(str.split('=')[0].replace(/s/g, ""));
                let value = decodeURIComponent(str.split('=')[1].replace(/s/g, ""));
                map.set(key, String(value));
            }
        }
    
        return map;
    }
    
    function setRequest(prefix, map) {
        let search = '';
        for (let [key, value] of map) {
            if (key != null && value != null) {
                key = encodeURIComponent(key);
                value = encodeURIComponent(value);
                search += `&${key}=${value}`;
            }
        }
    
        return encodeURI(url + '?' + search.substr(1));
    }
    
    /*
    encodeURIComponent:会转义特殊符号,用在外层则特殊符号也转义
    encodeURI:不会转义特殊符号,用在内层如果字段有特殊符号不转移会导致解析时无法分割
    /   -> %2F
    ?   -> %3F
    &   -> %26
    =   -> %3D
    #   -> %23
    空格 -> %20
    %   -> %25
    
     */
    
    // eg
    
    let map = new Map();
    map.set('a', '1');
    map.set('b', 1);
    map.set('c', true);
    location.href = setRequest('wwww.baidu.com', map);
    
    let map1 = getRequest();
    let a1 = map1.get('a');
    let b1 = map1.get('b');
    let c1 = map1.get('c');
    
  • 相关阅读:
    第五周
    第四周
    第三周作业
    第二周编程总结
    编程总结(3)
    编程总结(2)
    编程总结(1)
    第七周作业
    第六周作业
    第五周作业
  • 原文地址:https://www.cnblogs.com/n031/p/14847396.html
Copyright © 2011-2022 走看看