Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Mar 2010
    Location
    New Jersey USA
    Posts
    81
    Thanks
    13
    Thanked 4 Times in 4 Posts

    Adding a countdown to a javascript alert

    Is it possible to add a countdown to a javascript alert? I have a dropdown list that if you select a person an alert appears. I would like that alert to have a count down until November 1st (in days) is that possible?

    Code:
    if (n == "Frederick Doot") {alert ("Hello Frederick, you have ...... days left ")}

  • #2
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes it is possible you can write a small function for this. I found some function from http://www.javascriptkit.com/script/...2/count2.shtml follow this url.
    And edit this line "document.forms.count.count2.value="Only "+dday+ " days, "+dhour+" hours, "+dmin+" minutes, and "+dsec+" seconds" to "return "Only "+dday+ " days, "+dhour+" hours, "+dmin+" minutes, and "+dsec+" seconds"

    and you can use it like this
    if (n == "Frederick Doot") {alert ("Hello Frederick, you have"+countdown(2013,11,01)+"days left ")}

    Chekc here I created for you http://jsfiddle.net/NXnKy/

    Thanks.
    If you need more help PLS reply to me.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Code:
    <script type = "text/javascript">
    
    Date.prototype.getDOY = function() {
    var onejan = new Date(this.getFullYear(),0,1);
    return Math.ceil((this - onejan) / 86400000);
    }
    
    
    var n = "Frederick Doot";
    if (n == "Frederick Doot") {
    var today = new Date();
    var DOY1 = today.getDOY()+1;   // Day of Year - 1st Jan is Day 0
    var oneNov = new Date(2013,10,1);  // 1st November 2013 - months in Javascript are 0-11
    var DOY2 = oneNov.getDOY()+1;   // Day of Year - 1st Jan is Day 0
    var diff = DOY2 -DOY1;
    if (diff >=0) {
    alert ("Hello Frederick, you have " + diff + " days left ");
    }
    }
    </script>
    alerts are regarded as obsolete - you should use DOM methods to display a message to your user.


    The type of football they play helps them to bat above their weight. - Commentator Radio Ireland

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    connollyc4 (10-18-2013)

  • #4
    New Coder
    Join Date
    Mar 2010
    Location
    New Jersey USA
    Posts
    81
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Thanks for your help! I have one more question. I want to add this to a drop down that when you select "Bill Gates" no alert appears but if you select Fred Doot that timed alert appears.

    Code:
    <select name="folks" id = "notpaid" onchange="showPic(this); owePayment()">
     <option value="Pick Your Name">Pick Your Name</option>
    <option value="Bill Gates">Bill Gates</option>
    <option value="Fred Doot">Fred Doot</option>
    <option value="Bob Smith">Bob Smith</option>
    Code:
    <script type = "text/javascript">function owePayment() {
    var n = document.getElementById("notpaid").value;
    if (n == "Fred Doot") {alert ("Hello Fred, You have *number of days* left!")}

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,591
    Thanks
    0
    Thanked 644 Times in 634 Posts
    Don't forget to remove all the debugging alerts from your page before you make it live on the web. Netscape 4 was the last browser where alerts served any purpose in live web pages - since then it has been possible to have the script update the page itself to display any desired content (including generating your own "alert" box that can have whatever appearance you like).
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    New Coder
    Join Date
    Mar 2010
    Location
    New Jersey USA
    Posts
    81
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Well the alert box still works and will still get the job done, I just needed some help trying to add a countdown to that alert box and Phillip showed me that it can be done. Now i just want that to happen only when i select a specific name from a drop down menu. if you have a better example of how this is done some other way, please share.

    Thanks
    Last edited by connollyc4; 10-19-2013 at 10:14 PM.

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,591
    Thanks
    0
    Thanked 644 Times in 634 Posts
    Alert boxes display an extra checkbox in some browsers to aid in debugging. Also they can be turned off in some browsers so they don't display at all.

    What you should be using in place of alert is innerHTML.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Just call the script I gave you if
    var n = document.getElementById("notpaid").value;
    if (n == "Fred Doot") { /// .............

    You ought to be able to do that by yourself.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    connollyc4 (10-28-2013)

  • #9
    New Coder
    Join Date
    Mar 2010
    Location
    New Jersey USA
    Posts
    81
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Thanks Phlip, Works great!


  •  

    Posting Permissions

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