boax
boax

Reputation: 11

save selector in cookie and get that cookie back

I want to ask the user in my webapp with a drop-down menu which school website they want to open, then save that in a cookie and automatically open that website next time they open the webapp. I'm not good at JS so please explain.

Thank's in advance.

<head>
<script>
 <!-- Cookie script -->
</script>
</head>
<body>
    <form>
        <select id="class">
          <option value="Choose">Choose</option>
          <option value="flah">School#1</option> <!-- Should redirect to site #1 -->
          <option value="june">School#2</option> <!-- Should redirect to site #2 -->
        </select>
        <button type="submit">V&auml;lj</button>
    </form>
</body>

Update:

<

form>
        <select id="class">
          <option value="Choose">Choose</option>
          <option value="flah">School#1</option> <!-- Should redirect to site #1 -->
          <option value="june">School#2</option> <!-- Should redirect to site #2 -->
        </select>
        <button type="submit" onclick="setCookie()">V&auml;lj</button>
    </form>

<script type="text/javascript" language="javascript">    
    function setCookie(cookiename, cookievalue, cookieexdays) {
        var d = new Date();
        d.setTime(d.getTime() + (cookieexdays*24*60*60*1000));
        var expires = "expires="+d.toUTCString();
        document.cookie = cookiename+ "=" + cookievalue+ "; " + expires;
    }

    function getCookie(cookiename) {
        var name = cookiename+ "=";
        var ca = document.cookie.split(';');
        for(var i=0; i<ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1);
            if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
        }
        return "";
    }

    function checkCookie() {
        var school = getCookie("SelectedSchool");
        if (school!= "") {
            //redirect to user to link
            alert("Welcome to the " + school);
        } else {
            user = prompt("Please choose your name:", "");
            if (school != "" && school != null) {
                setCookie("SelectedSchool", school, 365);
            }
        }
    }
</script>

Upvotes: 1

Views: 330

Answers (2)

HaveNoDisplayName
HaveNoDisplayName

Reputation: 8497

Try this, These are the JavaScript function which you can use to save, get or check cookie

<head>
<script>
 <!-- Cookie script -->
</script>
</head>
<body>
    <form>
        <select id="class">
          <option value="Choose">Choose</option>
          <option value="flah">School#1</option> <!-- Should redirect to site #1 -->
          <option value="june">School#2</option> <!-- Should redirect to site #2 -->
        </select>
        <button type="submit">V&auml;lj</button>
    </form>

    <script type="text/javascript" language="javascript">
        function setCookie(cookiename, cookievalue, cookieexdays) {
            var d = new Date();
            d.setTime(d.getTime() + (cookieexdays*24*60*60*1000));
            var expires = "expires="+d.toUTCString();
            document.cookie = cookiename+ "=" + cookievalue+ "; " + expires;
        }

        function getCookie(cookiename) {
            var name = cookiename+ "=";
            var ca = document.cookie.split(';');
            for(var i=0; i<ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1);
                if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
            }
            return "";
        }

        function checkCookie() {
            var school = getCookie("SelectedSchool");
            if (school!= "") {
                //redirect to user to link
                alert("Welcome to the " + school);
            } else {
                user = prompt("Please choose your name:", "");
                if (school != "" && school != null) {
                    setCookie("SelectedSchool", school, 365);
                }
            }
        }
    </script>
</body>

Upvotes: 0

hatzaviv
hatzaviv

Reputation: 159

To create a cookie use:

document.cookie="key=value";

To get the value of the select use (example in jquery):

var val = $("#class").val();

Save that in the cookie. Now next time the user logs in read the cookie using:

var x = document.cookie;

and redirect him with:

location.href = "link";

good luck

Upvotes: 1

Related Questions