Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if browser supports "plaintext-only" value in contenteditable parameter?

I can't find any relevant information about "contenteditable" HTML5 parameter. I've found out, that Google Plus is using this for Chrome browsers:

<div contenteditable="plaintext-only"></div>

It seems that no other browsers support this and it's only Chrome proprietary value. I want to use it in my project. However, I need to detect the browser and find out if supports "plaintext-only" setting.

Of course, I could detect only Chrome, but there might be other browsers that support it (I don't know about any at this moment) or other main stream browsers might start supporting this feature in future.

So I would rather detect if the "plaintext-only" functionality is supported than detecting just Chrome browser.

Anyone can help me on this ?

like image 918
Frodik Avatar asked May 20 '12 08:05

Frodik


1 Answers

Here's an alternative if you prefer not to rely on catching exceptions to detect features:

function supportsPlaintextEditables () {
    var div = document.createElement('div');
    div.setAttribute('contenteditable', 'PLAINTEXT-ONLY');

    return div.contentEditable === 'plaintext-only';
}

console.log(supportsPlaintextEditables);
//-> true/false

This works because setting the value to the attribute instead of the property will not throw a SyntaxError exception if 'plaintext-only' is an invalid value, instead it will set the property value to the default, 'inherit'.

Getting the property after setting the attribute results in a lower-cased string, so setting the attribute value to 'PLAINTEXT-ONLY' will result in a property with the value 'plaintext-only' (supported/true) or 'inherit' (not supported/false).

like image 181
Andy E Avatar answered Oct 30 '22 14:10

Andy E