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?
<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
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
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.
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.
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;
}
}
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.
Apart from several syntax problems, the major issue is this:
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:
var deliveryDays = {
'Page Title 1': 3,
'Page Title 2': 6
};
d.addBusDays(deliveryDays[document.title]);