Provo a spiegarti il mio problema, magari c'è una soluzione alternativa. Ho un iframe in cui l'utente può inserire del testo, e tramite dei bottoni anche dei tag HTML (a, img...)

Per inserire il testo sto usando questo script:
codice:
function insertAtCursor(iframename, text, replaceContents)
 {
  if(replaceContents==null)
   {replaceContents=false;}
  
  if(!replaceContents)
   {
    sel = document.getElementById(iframename).contentWindow.getSelection();      
    sel.collapseToStart();
      
    document.getElementById(iframename).contentWindow.document.execCommand('insertHTML', false, text);
   } else {
    document.getElementById(iframename).contentWindow.document.execCommand('insertHTML', false, text);
   }
 }
Ma ho il seguente problema, se l'utente non seleziona prima l'iframe ho un errore. Invece io vorrei una cosa del tipo. Se il focus non è settato, scrivi nel iframe.

é possibile?