隐藏使用URL参数时发生的所有内容异常

时间:2014-10-03 20:17:46

标签: javascript html cookies

我想隐藏在网址中使用?token_code=12345678时所做的网站内容例外。这是代码无法正常工作,它隐藏了网站但从未显示过:

我通过 www.example.com/?authtoken=12345678

调用脚本

因此,当该参数包含在URL中时,它应该显示网站。但它没有显示它。它只隐藏它。

PS。我使用cookies来记住"令牌" :)

HTML:

<body data-token="12345678"> </body>

JS:

//setCookie and readCookie 
function SetCookie(e, t, n) {
    var r = new Date;
    var i = new Date;
    if (n == null || n == 0) n = 1;
    i.setTime(r.getTime() + 36e5 * 24 * n);
    document.cookie = e + "=" + escape(t) + ";expires=" + i.toGMTString()
}

function ReadCookie(e) {
    var t = " " + document.cookie;
    var n = t.indexOf(" " + e + "=");
    if (n == -1) n = t.indexOf(";" + e + "=");
    if (n == -1 || e == "") return "";
    var r = t.indexOf(";", n + 1);
    if (r == -1) r = t.length;
    return unescape(t.substring(n + e.length + 2, r))
}

function DeleteCookie(name) {
    document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}

//capitalzies string
function capitalize(str) {
    var first = str.charAt(0).toUpperCase();
    str = str.replace(/^.{1}/, first);
    return str;
}

// get's the GET paramters like so -->  $_GET('var1');
function getVar(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
    return (false);
}

// Checks for one of TWO access short codes
// includeOnly && excludeOnly 

// If includeOnly is not NULL, then ONLY include 
// categories mentioned in that varaible.
// Also, cookie the data, that it's saved.

// Of course, if anyone re-visits the site, and
// re-writes the GET paramter, it'd delete all
// previous data in the cookie.

var token_code = ["authtoken", "excludeOnly"];
var asc = ""; //this is used to select the CURRENT access short code
var tokenValues = [];

//first check if there are ANY get params.
if (getVar(token_code[0]) != false) {
    //before writing the inlcude only, delete EXCLUDE only
    DeleteCookie(token_code[1]);

    SetCookie(token_code[0], getVar(token_code[0]));
}
if (getVar(token_code[1]) != false) {
    //before writing the EXCLUDE only, delete include only
    DeleteCookie(token_code[0]);

    SetCookie(token_code[1], getVar(token_code[1]));
}

//Try and reaad the cookie (there should be a cookie named "includeOnly" or "excludeOnly -- both from token_code)

//includeOnly is present?
if (ReadCookie(token_code[0]).toString().length > 0) {
    //defines what the user wants to do. Exlcude or include? when token_code[0] it's include!
    asc = token_code[0];

    var tokens = ReadCookie(asc).toString();

    tokenValues = decodeURIComponent(tokens).split(',');

    //loop through each category.

    //hide every category and it's children
    $("[data-token]").hide();

    $.each(tokenValues, function (index, value) {
        //show every category, and it's childen, for the values
        $("[data-token='" + value + "']").show();
    });
}

//excludeOnly is present?
if (ReadCookie(token_code[1]).toString().length > 0) {
    //defines what the user wants to do. Exlcude or include? when token_code[0] it's include!
    asc = token_code[1];

    var tokens = ReadCookie(asc).toString();

    tokenValues = decodeURIComponent(tokens).split(',');

    //loop through each category.

    //hide every category and it's children
    $("[data-token]").show();

    $.each(tokenValues, function (index, value) {
        //show every category, and it's childen, for the values
        $("[data-token='" + value + "']").hide();
    });
}

有更简单的方法吗?

1 个答案:

答案 0 :(得分:0)

在代码的底部,如果评论显示,它会运行.hide()。 这可能是个问题吗?

//show every category, and it's childen, for the values
$("[data-token='" + value + "']").hide();