SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Evangelist
    Join Date
    May 2005
    Posts
    411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    .js displays date based on page title.

    I use this code to produce a date... however, I need to produce a different date depending on the page title... for instance: Page title: Product 1 will display 5/29/11. Page Title: Product 2 will display 6/13/11 etc.

    Here is the code I use to produce the date... any tips on how to get a different date for the different pages?

    Code:
    <p align="center"><br><b>Est. Shipping Date:</b><br> <br>
    <script type="text/javascript">
    Number.prototype.mod = function(n) {
    return ((this%n)+n)%n;
    }
    Date.prototype.addBusDays = function(dd) {
    var wks = Math.floor(dd/5);
    var dys = dd.mod(5);
    var dy = this.getDay();
    if (dy === 6 && dys > -1) {
       if (dys === 0) {dys-=2; dy+=2;}
       dys++; dy -= 6;}
    if (dy === 0 && dys < 1) {
       if (dys === 0) {dys+=2; dy-=2;}
       dys--; dy += 6;}
    if (dy + dys > 5) dys += 2;
    if (dy + dys < 1) dys -= 2;
    this.setDate(this.getDate()+wks*7+dys);
    }
    
    var d = new Date();
    d.addBusDays(3);
    var curr_date = d.getDate();
    var curr_month = d.getMonth();
    curr_month++;
    var curr_year = d.getFullYear();
    document.write(curr_month + "/" + curr_date + "/" + curr_year);
    </script>5/30/2011

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Gitman View Post
    I use this code to produce a date... however, I need to produce a different date depending on the page title... for instance: Page title: Product 1 will display 5/29/11. Page Title: Product 2 will display 6/13/11 etc.

    Here is the code I use to produce the date... any tips on how to get a different date for the different pages?
    What determines whether it should be 5/29/11 or 6/13/11 ?

    Are you wanting to retrieve the title of the page and extract the date part from there?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Evangelist
    Join Date
    May 2005
    Posts
    411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    What determines whether it should be 5/29/11 or 6/13/11 ?
    I think it's this line: d.addBusDays(3); if you change that 3 to a different number it will display a different date... basically it pulls in today's date and then adds days to it and then displays a projected date. So for instance, today this code will display 5/30/11 because that is 3 business days from now. If I change that 3 to a 6 it will display the date 6 business days from now i.e. 6/2/11

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Gitman View Post
    I think it's this line: d.addBusDays(3); if you change that 3 to a different number it will display a different date... basically it pulls in today's date and then adds days to it and then displays a projected date. So for instance, today this code will display 5/30/11 because that is 3 business days from now. If I change that 3 to a 6 it will display the date 6 business days from now i.e. 6/2/11
    So . . . what are you asking help with exactly?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Evangelist
    Join Date
    May 2005
    Posts
    411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need to basically change that variable based on the page title... so if page title is: Product 1 it should be d.addBusDays(3); if the page title is: product 2 it would be d.addBusDays(6); etc.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Gitman View Post
    I need to basically change that variable based on the page title... so if page title is: Product 1 it should be d.addBusDays(3); if the page title is: product 2 it would be d.addBusDays(6); etc.
    Okay, well first off, do not store the list of products and the number of business days within javascript, otherwise you will need to update that scripting code every time a product change occurs.

    Instead, keep the business days within the database along with the products.

    Once you've done that, you can use the server-side code to update this particular line with it's appropriate number of days.

    Code:
    d.addBusDays(3);
    Perhaps with something like:

    Code php:
    $businessDays = intval($productInfo['businessDaysToDeliver']);
    echo 'd.addBusDays(' . $businessDays . ');';
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Evangelist
    Join Date
    May 2005
    Posts
    411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    we use asp can't use php and there are very few pages this needs to be used on and the products don't change but like once a year, so a simple little js will work just fine... I just need to be able to specify the page title and variable in the JS like Page Title 1 = 3.... Page Title 2 = 6. so that the number in this line: d.addBusDays(6); changes based on the page title.

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Gitman View Post
    we use asp can't use php and there are very few pages this needs to be used on and the products don't change but like once a year, so a simple little js will work just fine... I just need to be able to specify the page title and variable in the JS like Page Title 1 = 3.... Page Title 2 = 6. so that the number in this line: d.addBusDays(6); changes based on the page title.
    Under protest that this is a bad way to manage your product information, you could use an associative array to store that info.

    Code javascript:
    var products = [
        {product: 'apple', businessDays: 3},
        {product: 'banana', businessDays: 6}
    ];

    and then use a loop to search through that array of objects, searching for a match with the page title.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    SitePoint Evangelist
    Join Date
    May 2005
    Posts
    411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How would I integrate that little bit of code into the code in my first post?

  10. #10
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Gitman View Post
    How would I integrate that little bit of code into the code in my first post?
    You would use a loop to search through that array of objects, searching for a match with the page title.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  11. #11
    SitePoint Evangelist
    Join Date
    May 2005
    Posts
    411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know how to code for a loop.

  12. #12
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Gitman View Post
    I don't know how to code for a loop.
    If you are not learning how to write code, then perhaps you should consider employing someone to do the work for you.
    Last edited by paul_wilkins; May 25, 2011 at 17:19.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  13. #13
    SitePoint Evangelist
    Join Date
    May 2005
    Posts
    411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this a loop?

    Code:
    var el = document.getElementById("someImage");
    var title = document.title;
    	for (var i=0;i<imgs.length;i++){
    		if (imgs[i].indexOf(title) != -1) {		
    			el.src = imgs[i];
    			break;	
    		}	
      }

  14. #14
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Gitman View Post
    Is this a loop?
    Yes, that is a loop, although it's not appropriate to declare a variable from within the loop. It's better for all variables to be declared at the start of their scope, so that there is no confusion about which variables are being used, and so that you don't become tempted to declare a variable from within the middle of a block of code.

    The != part is a bad habit - you should instead use !== so that unintended matches don't have the possibility of occurring.

    Run the code through jslint.com and turn on "The Good Parts" before you lint the code. Fix the errors that it finds (there will be a lot of them) and don't be afraid to ask us about any of the fixes that you don't understand.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  15. #15
    SitePoint Evangelist
    Join Date
    May 2005
    Posts
    411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Am I on the right track?

    Code:
    <p align="center"><br><b>Est. Shipping Date:</b><br> <br>
    <script type="text/javascript">
    Number.prototype.mod = function(n) {
    return ((this%n)+n)%n;
    }
    Date.prototype.addBusDays = function(dd) {
    var wks = Math.floor(dd/5);
    var dys = dd.mod(5);
    var dy = this.getDay();
    if (dy === 6 && dys > -1) {
    if (dys === 0) {dys-=2; dy+=2;}
    dys++; dy -= 6;}
    if (dy === 0 && dys < 1) {
    if (dys === 0) {dys+=2; dy-=2;}
    dys--; dy += 6;}
    if (dy + dys > 5) dys += 2;
    if (dy + dys < 1) dys -= 2;
    this.setDate(this.getDate()+wks*7+dys);
    }
    
    var d = new Date();
    var products = {
     'Page Title 1':{ businessDays: 3},
     'Page Title 2':{ businessDays: 6}
    };
    d.addBusDays(products[document.title]);
    var curr_date = d.getDate();
    var curr_month = d.getMonth();
    curr_month++;
    var curr_year = d.getFullYear();
    document.write(curr_month + "/" + curr_date + "/" + curr_year);
    </script>

  16. #16
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Apart from several syntax problems, the major issue is this:

    Code:
    var products = {
     'Page Title 1':{ businessDays: 3},
     'Page Title 2':{ businessDays: 6}
    };
    d.addBusDays(products[document.title]);
    Because you are using the title as the key, there can be some title names that aren't valid as array keys though, so be careful with that.

    You don't need an object for the number of days anymore though. However, you should now use a more appropriate array name to indicate just what those numbers mean:

    Code javascript:
    var deliveryDays = {
     'Page Title 1': 3,
     'Page Title 2': 6
    };
    d.addBusDays(deliveryDays[document.title]);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  17. #17
    SitePoint Evangelist
    Join Date
    May 2005
    Posts
    411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SWEEETNESS, works perfectly - thanks.


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
  •