corey toolis
corey toolis

Reputation: 13

Attempting to save cookie

I am trying to save a querystring as a cookie here. I am using the querystring for a login and i need to save the querystring to use it on another page. For some reason the code is not working correctly. Without the cookie stuff it will do what i want it to. But when i try and add that code it wont work anymore.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
    var QueryString = function() {
        // This function is anonymous, is executed immediately and 
        // the return value is assigned to QueryString!
        var query_string = {};
        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 first entry with this name
            if (typeof query_string[pair[0]] === "undefined") {
                query_string[pair[0]] = pair[1];
                // If second entry with this name
            } else if (typeof query_string[pair[0]] === "string") {
                var arr = [query_string[pair[0]], pair[1]];
                query_string[pair[0]] = arr;
                // If third or later entry with this name
            } else {
                query_string[pair[0]].push(pair[1]);
            }
        }
        return query_string;
    }();
    setCookie(cname, cvalue, exdays) {
        var d = new Date();
        d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
        var expires = "expires=" + d.toGMTString();
        document.cookie = cname + "=" + cvalue + "; " + expires;
    }
    setCookie("email", QueryString.u, 1);

    setCookie("password", QueryString.p, 1);

    jQuery(function() {
        jQuery('#email').val(QueryString.u);
        jQuery('#password').val(QueryString.p);
        jQuery('#submit').click();
    });
</script>

Upvotes: 0

Views: 101

Answers (2)

timat3d
timat3d

Reputation: 36

Try changing

setCookie(cname,cvalue,exdays)

to

function setCookie(cname,cvalue,exdays)

Upvotes: 1

Paulo Lima
Paulo Lima

Reputation: 1238

I believe it is due to his code is in error, it should not be?

var query_string = {};
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 first entry with this name
    if (typeof query_string[pair[0]] === "undefined") {
        query_string[pair[0]] = pair[1];
        // If second entry with this name
    } else if (typeof query_string[pair[0]] === "string") {
        var arr = [query_string[pair[0]], pair[1]];
        query_string[pair[0]] = arr;
        // If third or later entry with this name
    } else {
        query_string[pair[0]].push(pair[1]);
    }
}


function setCookie(cname, cvalue, exdays)
{
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}

and what is "QueryString"?

Upvotes: 1

Related Questions