Friday, September 19, 2008

Yet more coding fun

Not that I frequent the Sun website much, okay at all, but I followed a link from Obsolete over on septicisle and read the story, or tried too as the page kept jumping as it loaded.

Firing up the visual element inspector I'm shocked to discover that they're actually using the 'right' sort of coding - it's all div elements (which some might consider highly appropriate) and it's XHTML transitional so all is serene.

That is until you open up the code itself and see all the embedded javascript lurking around. At least the CSS class names are descriptive "padding-bottom-5" and "red-dotted-line" so easy to use until you want to change "red-dotted-line" to "red-solid-line" at which point you need to change the page generation code rather then just the css page which was one of the benefits of why you're supposed to be doing it that way.

So a hint of the embedded javascript

var bDisableRightClick=true; //whether to disable right click or not. True will disable the right click and false will enable it.
and
//Cma user can change the copyright messgae which will be displayed on right click on images.
or
//REQUIRED SECTION. CHANGE "YOURSERVER" TO VALID LOCATION ON YOUR WEB SERVER (HTTPS IF FROM SECURE SERVER)
Sweet! L33t users to be found here. From their javascript sol.js here's how they calculate the date to display
dServerDate = new Date();
var nDay;
var nDate;
var nMonth;
var nFullYear;
function fCreateDate(){
var sDate = '';
nDay = dServerDate.getUTCDay();
nMonth = dServerDate.getUTCMonth();
nDate = dServerDate.getUTCDate();
nFullYear = dServerDate.getUTCFullYear();
switch(nDay){
case 0:
sDate += 'Sunday';
break;
case 1:
sDate += 'Monday';
break;
case 2:
sDate += 'Tuesday';
break;
case 3:
sDate += 'Wednesday';
break;
case 4:
sDate += 'Thursday';
break;
case 5:
sDate += 'Friday';
break;
case 6:
sDate += 'Saturday';
break;
default:
}
sDate += ', ';
switch(nMonth){
case 0:
sDate += 'January';
break;
case 1:
sDate += 'February';
break;
case 2:
sDate += 'March';
break;
case 3:
sDate += 'April';
break;
case 4:
sDate += 'May';
break;
case 5:
sDate += 'June';
break;
case 6:
sDate += 'July';
break;
case 7:
sDate += 'August';
break;
case 8:
sDate += 'September';
break;
case 9:
sDate += 'October';
break;
case 10:
sDate += 'November';
break;
case 11:
sDate += 'December';
break;
default:
}
sDate += ' ';
sDate += nDate + ', ' + nFullYear;
document.getElementById('masthead-date').innerHTML = sDate;
}
Following that even for non-coders shouldn't be too difficult if I explain that getUTCDate and getUTCMonth return a number and that "+=" means take that variable and add this to it. See it's easy...
function fCreateDate(){
dServerDate = new Date();
DayName=new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
MonthName=new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
document.getElementById('masthead-date').innerHTML =
DayName[dServerDate.getUTCDay()]+", "+MonthName[dServerDate.getUTCMonth()]+" "+dServerDate.getUTCDate()+", "+dServerDate.getUTCFullYear();
}
Yep really easy. So with this quality in mind I gave up my attempt to discover the original problem.

0 comments: