0

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

1

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.