Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

javascript window对象 #51

Open
threetown opened this issue Nov 26, 2018 · 0 comments
Open

javascript window对象 #51

threetown opened this issue Nov 26, 2018 · 0 comments

Comments

@threetown
Copy link
Contributor

threetown commented Nov 26, 2018

window.location

function urlController(url) {
    var _url = url.split("?")[1];
    if (!_url) {
        return {};
    }
    var windowHref = _url.split("&");
    var obj = {};
    for (var i = 0; i < windowHref.length; i++) {
        var arr = windowHref[i].split("=");
        obj[arr[0]] = decodeURIComponent(arr[1])    // 路由解码
    }
    return obj;
}
  • 对象转路由
function objectToQueryString(obj) {
        var qstring = "?", i = 0;
        for (var key in obj) {
            if(i>0) qstring += "&";
            qstring += key + "=" + obj[key];
            i++;
        }
        return qstring
}
  • 下载
triggerDownload: function(content, filename) {
        var eleLink = document.createElement('a');
        eleLink.download = filename;
        eleLink.style.display = 'none';
        eleLink.href = content;
        document.body.appendChild(eleLink);
        eleLink.click();
        document.body.removeChild(eleLink);
}
  • array object
var tools = {
jsonToArray: function (data) {
        var arr = [];
        for (var key in data) {
            arr.push(data[key])
        }
        return arr;
    },
    arrayToJson: function(data, key){
        var obj = {};
        var keyV = key ? key : 'code';
        data.map(function (e, item) {
              obj[e[keyV]] = e;
        });
        return obj;
    },
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant