Document.activeElement returns body in Chrome and Safari

On the page, I have an Iframe that contains the input fields, and if I select one of these fields in FireFox and use it document.activeElement, I get an IFrame. This is normal, I just use this IFrame and get its contentDocument, save it, and then activate ActiveElement again, and now I get the input window.

However, in Chrome and Safari, when I select one of the fields inside the IFrame and do document.activeElement, I get a body element. If I select an item outside of the IFrame, it document.activeElementworks fine.

How can I get the active element in my case?

+3
source share
1 answer

, , . , , .

function showme() {

    var currentDoc = document;

    if (document.activeElement == document.body) {
        currentDoc = window.frames['child-iframe'].document;
    }

    if (currentDoc.activeElement.type == "text" 
        || currentDoc.activeElement.type == "textarea" 
        || currentDoc.activeElement.type == "checkbox") {
        currentDoc.activeElement.style.color = "red";
    }

}


window.onload = function() {
    setTimeout("showme()", 5000);
}
+1

Source: https://habr.com/ru/post/1761970/