www.webdeveloper.com
Results 1 to 6 of 6

Thread: Cookies and Alert Boxes

Hybrid View

  1. #1
    Join Date
    Apr 2007
    Location
    Minnesota
    Posts
    17

    Unhappy Cookies and Alert Boxes

    HI Everyone,

    Its been a while since I've been here. I need some help. I am in need of a JS code which can set a cookie for a domain so that its active while a user is visiting the site and its pages, but upon leaving the site by any method (ie closing the window or leaving the domain) an alert box with a message appears. The alert box should only popup once, when they leave, not when they go to each page of the site. I am a designer so my basic knowledge of JS won't do in this instance. IF anyone can please help I'd be a happy designer.

    Thanks and happy coding.
    ~Metallifan15

  2. #2
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    This would require click monitoring, sites within your domain do not trigger the event whereas clicking on a domain that is external would result in the pop up...

    So
    Code:
    function processThisClick(cl){
    yourLoc = window.location.hostname; // gets www.domain.com for this page
    if( yourLoc != cl.href ){ // cl.href would need to be stripped to its domain.
    // this bit runs because the link domain is external to the current web page domain.
     writeCookie("somecookiename",somedata); // a cookie setting function
     alert("Your leaving!"); // pop up a message
    }
    We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
    Code:
    Youre = {
          STILL_not_getting_it:function(){
               alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
          },
          MissingThePoint:function(msg){
                alert("You're missing the point. " + msg);
          }
    }
    Youre.STILL_not_getting_it();

  3. #3
    Join Date
    Apr 2007
    Location
    Minnesota
    Posts
    17
    That looks easy enough JunkMale. Do I need to worry about what goes into the cookie, or just let it be?

    Also, what changes do I have to make to which lines of the code?

    Please and thanks!
    ~Metallifan15

  4. #4
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Well its an idea plucked out my head to point you in a direction. I have no idea about your code as you never posted any!

    As for what I posted, again, idea, not testes and I don't expect that particular function to work as it IMHO needs more to it.

    My best advice is to sit down, write out the easy bits so you got something to work with and then look at the tough bits as smaller jobs. Breaking a program in to elements or lots of smaller functions to ease your coding will help you develop you script quicker than sitting and banging your head with trying to figure out how something is done.

    Its far better you expend your time and energies on breaking the job down and building the stuff you know then deal with the other later.
    We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
    Code:
    Youre = {
          STILL_not_getting_it:function(){
               alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
          },
          MissingThePoint:function(msg){
                alert("You're missing the point. " + msg);
          }
    }
    Youre.STILL_not_getting_it();

  5. #5
    Join Date
    Apr 2007
    Location
    Minnesota
    Posts
    17

    Code Snippet

    Code:
    <script type='text/javascript'>
    (function()
    {
      if( !/(^|\s|;)seenAlert=yes(;|\s|$)/.test( document.cookie ) )
      {
        if( !alert('"The Phrase That Pays" Call us and receive the phrase that pays to get $200 off our best price on any new or pre-owned vehicle in stock or a $100 gas card with any purchase 507-732-5127  Only valid for 48 hours after you receive the secret phrase.') )
        {
         history.back();
         location.href = document.referrer;
        }
        else
         document.cookie="seenAlert=yes";
      }
    
    })();
    
    </script>
    ~Metallifan15 :cool:
    This is a code I found on another thread here. Perhaps just some help modifying this to fit the above needs is acceptable . Again, I'm a designer not a developer so JavaScript is not my strength. I can understand how this basic code works, but I would like it to appear as mentioned initially, that the alert box appears when the viewer leaves the domain OR closes the window. And only appears once per session. So when the browser is closed the cookie is removed.

    Thanks!

  6. #6
    Join Date
    Apr 2007
    Location
    Minnesota
    Posts
    17

    Help Modifying Code

    Alright, I have a basic code I found at JavaScript Kit

    I would kindly like some help getting code to open an alert box when they close or leave a certain domain. The following script is from the above mentioned site.

    Code:
    <script type="text/javascript">
    //Get cookie routine by Shelley Powers
    function get_cookie(Name) {
    var search = Name + "="
    var returnvalue = "";
    if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    // if cookie exists
    if (offset != -1) {
    offset += search.length
    // set index of beginning of value
    end = document.cookie.indexOf(";", offset);
    // set index of end of cookie value
    if (end == -1) end = document.cookie.length;
    returnvalue=unescape(document.cookie.substring(offset, end))
    }
    }
    return returnvalue;
    }
    
    function setcolor(what){
    document.body.style.backgroundColor=what
    document.cookie="bgcolor="+what
    }
    
    if (get_cookie("bgcolor")!="")
    document.body.style.backgroundColor=get_cookie("bgcolor")
    
    </script>
    Is there a way to modify this section
    Code:
    function setcolor(what){
    document.body.style.backgroundColor=what
    document.cookie="bgcolor="+what
    }
    
    if (get_cookie("bgcolor")!="")
    document.body.style.backgroundColor=get_cookie("bgcolor")
    so that it calls up an alert box?

    Please help. I'm not a JS expert I only have basic knowledge and right now I do not have resources or time to fully "learn" the expansive amount of JS knowledge to create this code myself.

    Thanks!
    Metallifan15

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles