Вопрос

There is a quirck in Internet Explorer that forces you to enclose element names with angle brackets when formatting text with execCommand and formatBlock. All other browsers accept document.execCommand("formatBlock", false, "H1") while Internet Explorer requires document.execCommand("formatBlock", false, "<H1>")

IE10 and below are detectable with the following trick:

isIE = function () {
    var userAgent   = navigator.userAgent,
        isIE        = userAgent.indexOf("MSIE") !== -1 && userAgent.indexOf("Opera") === -1;

    return isIE;
}

Internet Explorer 11 breaks browser detection by pretending to be Firefox. I know it is frowned upon to do browser detection and I should do feature detection instead. In this case the feature is there but it is not consistent.

Is there another way to consistently apply block formatting without relying on browser detection?

Это было полезно?

Решение

execCommand() returns true if the command is successful, or false otherwise. Hence you can do something like this:

if (!document.execCommand('FormatBlock', null, 'H1')) {
    document.execCommand('FormatBlock', null, '<H1>');
}

Though other browsers seem also to work with <H1> as well...

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top