Reputation: 15
Is there a way for a native (no jquery or libs) JavaScript function to detect whether a browser has CSS turned off?
Its possible to turn off CSS so am looking for a function to check.
Please help
Upvotes: 0
Views: 80
Reputation: 4179
What about a brute force style test like:
function testCss(){
var div, width, flag = true;
div = document.createElement('div');
document.body.appendChild('div');
div.style.width = "10";
if (div.offsetWidth != 10){
flag = false;
}
div.style.width = "20px";
if (div.offsetWidth != 20){
flag = false;
}
document.body.removeChild(div);
return flag;
}
The above sets a div elements width.
Checks to see if the div in question has the width just set.
Then repeats the same again but with 20px width, just incase it was already set with 10px width.
Lastly it removes the div.
There is most likely a better way to do it natively but this was a quick solution.
Not %100 sure that works but thats the general idea.
Hope it helps
Upvotes: 2
Reputation: 2914
From the same link I posted above,
var cssdisabled = false; // must be proven otherwise
var testcss = document.createElement('div');
testcss.style.position = 'absolute';
document.getElementsByTagName('body')[0].appendChild(testcss);
if (testcss.currentStyle) var currstyle = testcss.currentStyle['position'];
else if (window.getComputedStyle) var currstyle = document.defaultView.getComputedStyle(testcss, null).getPropertyValue('position');
var cssdisabled = (currstyle == 'static') ? true : false;
document.getElementsByTagName('body')[0].removeChild(testcss);
Upvotes: 0