...

View Full Version : Can anyone find a bug in this or a way to improve it ?



Ant-B
09-07-2011, 04:30 PM
function textSize(updown) {
var size = Number(readCookie('user-font-size'));
if (size == 0) {
size = 12; // No Cookie, Set A Default Size Of 12px
}
size = size + Number(updown); // Adjust The Current Size
if (size > 20) { size = 20; } // Stop At 13
if (size < 12) { size = 12; } // Stop At 10
writeCookie('user-font-size', size, 12); // Update The Cookie
document.body.style.fontSize = size + 'px'; // Set The Users Text Size
}


function writeCookie(name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = '; expires=' + date.toGMTString();
} else {
expires = '';
}
document.cookie = name + '=' + value + expires;
}

function readCookie(name) {
name += '=';
var cs = document.cookie.split(';');
for (var i = 0; i < cs.length; i++) {
var c = cs[i];
while (c.charAt(0) == ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(name) == 0) { //-----------//
return c.substring(name.length, c.length); // Gotcha ;) //
} //-----------//
} //------------//
return null; // Failed :( //
} //------------











<img id="plustext" class="zoom-in" alt="Increase text size" src="Zoom-In-icon.png" onclick="textSize (2);" style="cursor:pointer" />
<img id="minustext" class="zoom-out" alt="Decrease text size" src="Zoom-Out-icon.png" onclick="textSize (-2);" style="cursor:pointer" />


<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer vel fringilla arcu. Cras ullamcorper nunc ac sapien gravida ultrices.
Curabitur mollis consectetur elit non tempus. Duis blandit luctus feugiat. Nullam eget tellus at quam dapibus tempus in scelerisque ligula.
Phasellus turpis ante, tincidunt at aliquam vel, luctus vitae leo. Suspendisse dignissim vehicula est, ac dapibus justo pellentesque eu.
</p>






cheers hope thats enough code ;)

Ant.

Old Pedant
09-07-2011, 07:17 PM
Well, if you are going to read multiple cookie values in the same page, then having to go back into the cookie string to get each one, one at a time, is inefficient. But not horribly so.

And I'd probably check the cookie value for null, rather than just assuming I can convert it to Number().



function textSize(updown)
{
var size = readCookie('user-font-size');
if ( ( size = size==null ? 0 : Number(size) ) == 0) {
...

p.s.: Please use [code] tags in postings instead of [icode] tags. [icode] tags are only useful for up to maybe one line of code.

Ant-B
09-08-2011, 10:00 AM
Cool thanks for the pointer there.

currently this piece of script only re sizes everything in the "body" tag, how could change this script to only re size a specific tag for example a <span></span>


any ideas?

cheers,
Ant.

Old Pedant
09-08-2011, 07:21 PM
function textSize(updown) {
...
writeCookie('user-font-size', size, 12); // Update The Cookie
var changes = document.getElementsByClassName("resizable");
for ( var c = 0; c < changes.length; ++c )
{
changes[c].style.fontSize = size + 'px';
}
}

That won't work for older MSIE browsers, as they don't support getElementsByClassName.

So you could do:


function textSize(updown) {
...
writeCookie('user-font-size', size, 12); // Update The Cookie
var spans = document.getElementsByTagName("span");
for ( var c = 0; c < changes.length; ++c )
{
if ( spans[c].className == "resizable" ) spans[c].style.fontSize = size + 'px';
}
}

And many variations on this theme.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum