SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Set Cookie and go to page

    Hi there,
    For the splash page of my company, the visitors are presented with 2 options- they can go either to the Flash version of the site or the HTML version. If a visitor clicks on the image for the flash version, I'd like to set a cookie on their machine so that the next time they visit the site, they will be automatically taken to the flash version. I would also like it if when they clicked the image, the cookie would be set and they would be taken to the flash page. I am only halfway there. The current script just sets the cookie. It'd be good if when they click the button the page refreshes. Please help me. Thanks!

    <script language="JavaScript" type="text/javascript">
    <!-- Begin
    var expDays = 365;
    var exp = new Date();
    exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

    function getCookieVal (offset) {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
    endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
    }
    function GetCookie (name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
    return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
    }
    return null;
    }
    function SetCookie (name, value) {
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : null;
    document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
    }
    function DeleteCookie (name) {
    var exp = new Date();
    exp.setTime (exp.getTime() - 1);
    var cval = GetCookie (name);
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
    }

    var favorite = GetCookie('phoenix');

    if (favorite != null) {
    switch (favorite) {
    case 'html' : url = 'html/index.html';
    case 'flash' : url = 'home.htm';
    break;
    }
    window.location.href = url;
    }
    </script>
    <input type="button" name="phoenix" onClick="SetCookie('phoenix', this.value, exp);" value="html"></p>
    <input type="button" name="phoenix" onClick="SetCookie('phoenix', this.value, exp);" value="flash">

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    location.reload()
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •