Search

Rss Posts

Rss Comments

Login

 

Posts from February 4th, 2009

Rewrite Document Titles and Fix Orphans with Javascript

Feb 04

One more doozy… let’s say your LMS was built without attaching id’s to anything.  How about one class for every piece of text on-screen? Enormous tables? Check. Please.

I wrote some classes I put around titles, which were nice enough to be duplicated from ‘display’ to ‘navigation’, cool! But now my title bar looks like this: <span class=”noShow”>Totally Uncool</span>

Which is weak. I had three classes I was using to further differentiate items on the naviation bar.  Here’s how I repaired it.

fix = document.title;
if (fix.indexOf(‘<span class=”noShow”>’) != -1){
fixMe = fix.split(‘<span class=”noShow”>’);
good = fixMe[1].substr(0, fixMe[1].indexOf(‘</span>’));
document.title = good;
}else if(fix.indexOf(‘<span class=”tC”>’) != -1){
fixMe = fix.split(‘<span class=”tC”>’);
good = fixMe[1].substr(0, fixMe[1].indexOf(‘</span>’));
document.title = good;
}else if(fix.indexOf(‘<span class=”stC”>’) != -1){
fixMe = fix.split(‘<span class=”stC”>’);
good = fixMe[1].substr(0, fixMe[1].indexOf(‘</span>’));
document.title = good;
}

Also, if you’ve worked with Print Layout People (you know who I am talking about… Kerning Snobs, etc.) or have some layout experience yourself, you’re familar with orphans.  Oh, woe unto them as they stand alone in a cold (line) world by their lonesome. I was just concerned with fixing them in titles, where they really attract unwanted attention.

(this does require Mootools)

//fix orphaned words in titles
var ti = $(document.body).getElement(‘h3.ti’);
if($(ti)){

//make sure we have something, otherwise IE freaks right out
var tiSize = $(ti).getSize();
var output;

//if it’s larger than two lines, attach a nbsp between last two words

/* In this case, my h3 is 18px. I am sure there is a clever way to get the info out of the style sheet, but I don’t know of it (yet). 28 accounts for padding and the fact that IE has different sized pixels than everybody else.*/
if (tiSize.y>28){
var tiText = $(ti).get(‘html’).split(‘ ‘);
if (tiText.length > 2){
for (r=2; r<=tiText.length-1; r++){
if(tiText[r].length > 0){
output += tiText[r];
if(r==tiText.length-2){
output += ‘ ’;
}else{
output += ‘ ‘;
}

}

}
he = tiText[0]+’ ‘+tiText[1]+’ ‘;
po = output.split(‘undefined’).join(”);

//just in case!!!
$(ti).set(‘html’, he+”+po);
}
}
}

So yeah, Mootools pretty much saved my bacon all over the place on this project.