Diciamo che bastava controllare se la variabile è piena o no

codice:
function insertAtCursor(iframename, text, replaceContents)
 {
  if(replaceContents==null)
   {replaceContents=false;}
  
  sel = document.getElementById(iframename).contentWindow.getSelection();
  
  if(sel.length>0)
   {
    if(!replaceContents)
     {
      sel.collapseToStart();
      
      document.getElementById(iframename).contentWindow.document.execCommand('insertHTML', false, text);
     }
    else
     {document.getElementById(iframename).contentWindow.document.execCommand('insertHTML', false, text);}
   }
  else
   {
     //scrittura forzata su iframe
   }
 }