samionyoung
4/9/2018 - 5:46 AM

js snippets

js snippets

function loadStyles(url) {
    const link = document.createElement("link");
    link.rel = "stylesheet";
    link.type = "text/css";
    link.href = url;
    const head = document.getElementsByTagName("head")[0];
    head.appendChild(link);
}
function loadScript(url) {
    const script = document.createElement("script");
    script.type = "text/javascript";
    script.src = url;
    document.body.appendChild(script);
}
function getQueryStringArgs() {
    // 取得查询字符串并去掉开头的问号
    const qs = location.search.length > 0 ? location.search.substring(1) : "";

    //保存数据的对象
    const args = {};

    //取得每一项
    const items = qs.length ? qs.split("&") : [];
    let item, name, value;
    for (let i = 0, len = items.length; i < len; i++) {
        item = items[i].split("=");
        name = decodeURIComponent(item[0]);
        value = decodeURIComponent(item[1]);

        if (name.length) {
            args[name] = value;
        }
    }

    return args;
}
//检测插件(在IE中无效)
function hasPlugin(name) {
    name = name.toLowerCase();
    for (let i = 0, len = navigator.plugins.length; i < len; i++) {
        if (navigator.plugins[i].name.toLowerCase().indexOf(name) > -1) {
            return true;
        }
    }
    return false;
}

//检测IE中的插件
function hasIEPlugin(name) {
    try {
        new ActiveXObject(name);
        return true;
    } catch (err) {
        return false;
    }
}

//检测所有浏览器中的flash
function hasFlash() {
    const result = hasPlugin("flash");
    if(!result) {
        result = hasIEPlugin("ShockWaveFlash.ShockWaveFlash");
    }
    return result;
}