SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: Confirmation

  1. #1
    SitePoint Zealot vbthanks's Avatar
    Join Date
    May 2001
    Location
    Sydney
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Confirmation

    Hello,
    I have 5 links on a page. When a user clicks one of them, I need a popup confirmation before it will proceed to that link.
    The reason for this is that I want to set a cookie (via php) when the user presses "OK", so that it will remember their selection next time they visit.
    A 'Redirect Page' with a memory, basically. Any help appreciated!
    Cheers,
    Mike
    "You know what you know - but that's all you know!"

  2. #2
    SitePoint Member
    Join Date
    Aug 2003
    Location
    Netherlands
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think it is a bad idea. I think it is better if you have a checkbox on your page with that choice.

    But it could be done quite easily, I guess:
    Code:
    <a href="etc" onclick="return yesorno();">etc</a>
    function yesorno(){
    var x=prompt('Yes or no?');
    if (x==true){setcookie();return true
    }
    else {return false;};
    }

  3. #3
    SitePoint Zealot vbthanks's Avatar
    Join Date
    May 2001
    Location
    Sydney
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks mw22 but this is not what I am looking for. Not looking for a prompt, but something with a 'Yes' and 'No' Box. (Confirm)
    If 'Yes'- set cookie and goto page.
    If 'No' - goto page, no cookie.
    Cheers,
    Mike
    "You know what you know - but that's all you know!"

  4. #4
    SitePoint Member
    Join Date
    Aug 2003
    Location
    Netherlands
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oops, sorry. Instead of prompt it should have been confirm. So type instead of prompt, confirm and it works the way you want.
    Regards, Martijn

  5. #5
    SitePoint Zealot vbthanks's Avatar
    Join Date
    May 2001
    Location
    Sydney
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi mw22,
    This isn't working for me. I want to set a cookie so that the user doesn't see the original page with the 5 links on it ever again.
    This is how it works...
    User goes to abc.com - there are 5 links.
    User clicks on link2.
    Confirm popup asks "Do you want this to be your default page?"
    If "Yes" User is redirected to link2.
    Sometime later when user returns to abc.com - they are automatically redirected to link2 without confirm popup.
    If user selects "No" - they goto link2 but no cookie is set.
    Hope you can help - Cheers,
    Mike
    "You know what you know - but that's all you know!"

  6. #6
    SitePoint Member
    Join Date
    Aug 2003
    Location
    Netherlands
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've made a working example. This should do as you described.
    The cookie functions are copied from scottandrew: http://scottandrew.com/weblog/jsjunk#cookie
    Code:
    <html>
    <head>
    <title>Untitled</title>
    <script type="text/javascript">
    function saveCookie(name,value,days)
    {
      if (days) {
        var d = new Date();
        d.setTime(d.getTime()+(days*24*60*60*1000));
        var ex = "; expires="+d.toGMTString();
      }
      else {
        var ex = "";
      }
      document.cookie = name+"="+value+ex+"; path=/";
    }
    
    function readCookie(name)
    {
      var eq = name + "=";
      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,c.length);
        if (c.indexOf(eq) == 0) return c.substring(eq.length,c.length);
      }
      return null;
    }
    
    function eraseCookie(name)
    {
      saveCookie(name,"",-1);
    }
    tempc=readCookie('link');
    
    if (((tempc!=null)&&(tempc!='')))window.location=tempc;
    
    function yesorno(){ var x=confirm('Yes or no?'); 
    if (x==true){saveCookie('link',this,365);
    };
    return true;  
    }
    
    function ilinks(){
    var x=document.links;
    for (var i=0;tempx=x.length,i<tempx;i++){
    x[i].onclick=yesorno;
    }
    }
    window.onload=ilinks;
    
    </script>
    </head>
    <body>
    <a href="test1">test1</a>
    <a href="test2">test2</a>
    <a href="test3">test3</a>
    <a href="test4">test4</a>
    <a href="test5">test5</a>
    <button onclick="eraseCookie('link')">erasecookie</button>
    </body>
    </html>
    Regards, Martijn

  7. #7
    SitePoint Zealot vbthanks's Avatar
    Join Date
    May 2001
    Location
    Sydney
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Martijn - you are a champion! It works perfectly [img]images/smilies/smile.gif[/img] As you can tell, I'm new to javascript.
    Some questions...
    How do I change the 'Erase Cookie' button into link ?
    How do I put the 'Erase Cookie' link on test1, test2, etc pages ? (So user can reset their cookie)
    If I want to change the name of the cookie, do I just change the value in the brackets of each function ? (Or all instances?)
    Regards,
    Mike
    Last edited by vbthanks; Aug 7, 2003 at 17:02.

  8. #8
    SitePoint Member
    Join Date
    Aug 2003
    Location
    Netherlands
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can write:
    <a onclick="eraseCookie('link');return false;" href="javascript://">erasecookie</a> to get a link instead of a button.

    You can just copy the eraseCookie (you have to copy also the saveCookie function) function on every page you want, and then make a link just like above, but with the eraseCookie function.

    You don't have to change the functions. You just have to change the name of the cookie name in the instance that you use it.

    Regards,
    Martijn

  9. #9
    SitePoint Zealot vbthanks's Avatar
    Join Date
    May 2001
    Location
    Sydney
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help Martijn.
    "...then make a link just like above, but with the eraseCookie function..."
    Not sure what you mean ? I tried this, but the page continuously refreshes. I am trying to put a link on test1, test2... etc so the user can delete their cookie. Here's test1.html....
    Code:
    <html>
    <head>
    <title></title>
    <script type="text/javascript">
    function saveCookie(name,value,days)
    {
      if (days) {
    	var d = new Date();
    	d.setTime(d.getTime()+(days*24*60*60*1000));
    	var ex = "; expires="+d.toGMTString();
      }
      else {
    	var ex = "";
      }
      document.cookie = name+"="+value+ex+"; path=/";
    }
    function eraseCookie(name)
    {
      saveCookie(name,"",0);
    }
    tempc=readCookie('link');
    if (((tempc!=null)&&(tempc!='')))window.location=tempc;
    function yesorno(){ var x=confirm('Is this your Triple M?'); 
    if (x==true){saveCookie('link',this,365);
    };
    return true;  
    }
    </script>
    </head>
    <body>
    This is test2<br>When the user clicks the link below, their cookie is deleted.<br>
    <a onclick="eraseCookie('link');return false;" href="java script://">erasecookie</a>
    </body>
    </html>
    Regards,
    Mike
    "You know what you know - but that's all you know!"

  10. #10
    SitePoint Member
    Join Date
    Aug 2003
    Location
    Netherlands
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, sorry. I haven't tested the script very much.
    This happens when the active page url itself is stored as the cookie value.
    Change this:
    Code:
    if (((tempc!=null)&&(tempc!='')))window.location=tempc;
    Into this:
    Code:
    if (((tempc!=null)&&(tempc!='')&&(tempc!=window.location.href)))window.location=tempc;
    You should not get a continuously refreshes now. I haven't tested it, although.


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
  •