JS常用积累

复制到剪贴板

function copyToClipboard(val) {
    let aux = document.createElement("input");
    aux.setAttribute("value", val);
    document.body.appendChild(aux);
    aux.select();
    document.execCommand("copy");
    document.body.removeChild(aux);
    showDefaultTips('已成功复制到剪贴板','',1)
}

获取url参数

function getQueryVariable() {
    let query = window.location.search.substring(1);
    let vars = query.split("&");
    let res = {};
    for (let i = 0; i < vars.length; i++) {
        let pair = vars[i].split("=");
        if (pair[0] !== "") {
            let val = decodeURIComponent(pair[1]);
            if (val.includes("|")) {
                val = val
                    .split("|")
                    .filter((item) => item !== "")
                    .join(",");
            }
            if (val === "null") val = "";

            res[pair[0]] = val;
        }
    }
    return res;
}

你可能感兴趣的:(javascript)