How can i catch the firefox spellcheck correction event?

断了今生、忘了曾经 提交于 2019-12-01 17:56:55

Mozilla fires oninput in this case, didn't test in others, but should work everywhere.

Interestingly enough, FF seems to fire two input events when using spelling correction: it deletes the word first, and then inserts the new one:

> value=[holy coww]
(right click and choose "cow")
> value=[holy ]
> value=[holy cow]

http://jsfiddle.net/7ssYq/

Andy E

I was originally going to suggest the oninput event, like thg435's answer, but I thought I'd fish for more details in the comments first. If you don't need to differentiate between spell checker corrections and other types of input (keyboard, paste, drag and drop, etc), then oninput would do the job just fine.

If you do want to differentiate between those types of input, then I'm afraid there's no event that fires specifically for spell checker corrections. However, there are events for most other types of input, so you could at least narrow down the likelihood of your input event being a correction if you check for other types of event first. Consider the following:

(function () {
    var el = document.getElementById("MyInput"),
        ignore = false;

    el.oninput = function (e) {
        // ignore the events that we don't need to capture
        if (ignore) {
            ignore = false;
            return true;
        }

        // Your code here
    }

    // IIRC, you need the following line for the `ondrop` event to fire
    el.ondragover = function () { return false; }

    // Ignore paste, drop and keypress operations
    el.onpaste = el.ondrop = el.onkeypress = setIgnore;

    function setIgnore (e) {
        ignore = true; 
    }
})();

This isn't a perfect solution, however. For instance, the event will still fire for Undo/Redo actions (and, perhaps some other actions) that aren't initiated by the keyboard.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!