I’m upgrading a script to make it crosss browser. The current code I have is as follows.
function testFocus(){
var testSelection = document.getElementById('chattext').contentWindow.
window.document.selection.createRange().parentElement();
while (testSelection)
{
if (testSelection.id == "chatContent") {
return true;
}
testSelection = testSelection.parentElement;
}
return false;
}
However the following code no longer works in modern browsers. Presently the code above has to have text selected. Where it just needs to check that the textbox has focus. The function is used as a check before text is added by a button / javascript.
Strikes me that you could use an event listener to set a variable. The only problem being that IE uses
attachEvent(event, callback)instead ofaddEventListnerso you’ve actually got to add the codeedit – I’ve added script tags to show you how it adds to your document. I’ve tested in firefox and chrome and it seems to work, but getting an IE sandbox together might be a little more difficult for me. I’m sure there’s something little that I’m missing there – i’ll take a look.
edit2 i made a mistake with the IE code. tuns out you don’t put quotes around
'undefined'I’ve fixed the code above to reflect an answer that is tested and wotkign in firefox, chrome and IE6. I don’t have any other IEs to test in.