I have this JavaScript that should set the class attribute of the DIV#main to e.g. "1200pluss" or whatever, depending on window width -- but I can't get it to work! This is the script:
(Feel free to ignore the bit corresponding to "theWidth > 800" -- that's my test line
The script detects the width just fine, and using an alert or a doc.write works, but not "setAttribute":\
A friend suggested using this instead:
... but that didn't work either.
I've put up a copy of the page in question here:
... and the script here:
... if you want to have a closer look at it.
I'd appriciate any help on the subject!
Code:
function getBrowserWidth()
{
if (window.innerWidth)
{
return window.innerWidth;
}
else if (document.documentElement && document.documentElement.clientWidth != 0)
{
return document.documentElement.clientWidth;
}
else if (document.body)
{
return document.body.clientWidth;
}
return 0;
}
function changeClass()
{
var theWidth = getBrowserWidth();
var theElement = document.getElementById("main");
if ( theWidth > 1400 )
{
theElement.setAttribute('class','1400pluss');
}
else if (theWidth > 1200)
{
theElement.setAttribute('class','1200pluss');
}
else if (theWidth > 800)
{
alert(theElement + ' 800px+');
}
else
{
theElement.setAttribute('class','fallback');
}
return true;
}
(Feel free to ignore the bit corresponding to "theWidth > 800" -- that's my test line
The script detects the width just fine, and using an alert or a doc.write works, but not "setAttribute":\
A friend suggested using this instead:
Code:
theElement.className='fallback';
... but that didn't work either.
I've put up a copy of the page in question here:
... and the script here:
... if you want to have a closer look at it.
I'd appriciate any help on the subject!