If user selection is null, editor1doc.selection.createRange().htmlText fails in Firefox?

Last post 06-25-2007, 6:10 PM by aguest. 6 replies.
Sort Posts: Previous Next
  •  06-13-2007, 6:44 PM 30703

    If user selection is null, editor1doc.selection.createRange().htmlText fails in Firefox?

    The following Javascript code works fine in IE, but in Firefox, if the user has not actually highlighted any content in the editor, the 'createRange()' statement appears to just fail (the Javascript function ends without the alert after the statement appearing).   Is there a way to test for a null user selection in Firefox so that I can just PasteHTML(Date()) instead of my PasteHTML(selectedhtml + Date()) in that case?
     
    function CE_AddDateTime()
    {
       //  Paste current date/time into current cursor-point of editor.
       var editor1 = document.getElementById('<%=Editor1.ClientID%>');
       alert("DEBUG: CE_AddDateTime, after definition of editor1");
       var editor1doc = editor1.GetDocument();
       alert("DEBUG: CE_AddDateTime, after definition of editor1doc");
       var selectedhtml = editor1doc.selection.createRange().htmlText;
       alert("DEBUG: CE_AddDateTime, after definition of selectedhtml");
       editor1.PasteHTML(selectedhtml + Date());
    }
  •  06-14-2007, 10:13 AM 30736 in reply to 30703

    Re: If user selection is null, editor1doc.selection.createRange().htmlText fails in Firefox?

    rockford,
     
    The following code will only work in IE.
     
    var selectedhtml = editor1doc.selection.createRange().htmlText;
    Only IE supports htmlText method.
     
    Replace the above code with the following code:
     
    function CE_AddDateTime()
    {
       //  Paste current date/time into current cursor-point of editor.
       var editor1 = document.getElementById('<%=Editor1.ClientID%>');
       alert("DEBUG: CE_AddDateTime, after definition of editor1");
       var editor1doc = editor1.GetDocument();

       var editdoc=editor1.GetDocument();  
       var editwin = editor1.GetWindow();

       alert("DEBUG: CE_AddDateTime, after definition of editordoc");
       var selectedhtml = getSelectedHTML();

       alert("DEBUG: CE_AddDateTime, after definition of selectedhtml");
       editor1.PasteHTML(selectedhtml + Date());

     

        function getSelectedHTML(){
          var rng=null,html="";

          if (document.selection && document.selection.createRange){
            rng=editdoc.selection.createRange();
            html=rng.htmlText||"";
          }else if (window.getSelection){
            rng=editwin.getSelection();

            if (rng.rangeCount > 0 && window.XMLSerializer){
              rng=rng.getRangeAt(0);
              html=new XMLSerializer().serializeToString(rng.cloneContents());
            }
          }
          return html;
        }
    }


     


    asp.net Chat http://cutesoft.net/ASP.NET+Chat/default.aspx
    Web Messenger: http://cutesoft.net/Web-Messenger/default.aspx
    asp.net wysiwyg editor: http://cutesoft.net/ASP.NET+WYSIWYG+Editor/default.aspx
    asp wysiwyg html editor: http://cutesoft.net/ASP
    asp.net Image Gallery: http://cutesoft.net/ASP.NET+Image+Gallery/default.aspx
    Live Support: http://cutesoft.net/live-support/default.aspx

  •  06-14-2007, 3:01 PM 30751 in reply to 30736

    Re: If user selection is null, editor1doc.selection.createRange().htmlText fails in Firefox?

    Thanks, Adam.   Will try this out.   What other functions/commands are specific to IE?
  •  06-14-2007, 4:18 PM 30758 in reply to 30751

    Re: If user selection is null, editor1doc.selection.createRange().htmlText fails in Firefox?

    >>>What other functions/commands are specific to IE?
     
    The DOM implementation is different from browser to browser. To get a whole list, you need to get a book and try it in the real life.
     
     

    asp.net Chat http://cutesoft.net/ASP.NET+Chat/default.aspx
    Web Messenger: http://cutesoft.net/Web-Messenger/default.aspx
    asp.net wysiwyg editor: http://cutesoft.net/ASP.NET+WYSIWYG+Editor/default.aspx
    asp wysiwyg html editor: http://cutesoft.net/ASP
    asp.net Image Gallery: http://cutesoft.net/ASP.NET+Image+Gallery/default.aspx
    Live Support: http://cutesoft.net/live-support/default.aspx

  •  06-17-2007, 12:35 AM 30831 in reply to 30736

    Re: If user selection is null, editor1doc.selection.createRange().htmlText fails in Firefox?

    I just today had the same problem (.htmlText not working in firefox), and found this page when I googled for help (sorry am no customer).
    Thanks for XMLSerializer-info. this works great!!

    In the following i am doing the same as the OP does with his "editor1.PasteHTML(selectedhtml + Date());" and run into the next problem which the OP may als get:
    when the end of the selection contains different formated text (i.e. bold) than at the beginning of the selection, in the variable "selectedhtml" this formated text gets closed (and reopended again just after the end of the selection).
    when using "selectedhtml + Date()" to add the Date, that text will be inserted just in between the closing and the opening-tags. so date wont be bold even when it should be.
    how is this solved?
  •  06-18-2007, 1:57 PM 30858 in reply to 30831

    Re: If user selection is null, editor1doc.selection.createRange().htmlText fails in Firefox?

    aguest,
     
    How did you implement the pasteHTML method? This method is different from browser to browser.
     
     

    asp.net Chat http://cutesoft.net/ASP.NET+Chat/default.aspx
    Web Messenger: http://cutesoft.net/Web-Messenger/default.aspx
    asp.net wysiwyg editor: http://cutesoft.net/ASP.NET+WYSIWYG+Editor/default.aspx
    asp wysiwyg html editor: http://cutesoft.net/ASP
    asp.net Image Gallery: http://cutesoft.net/ASP.NET+Image+Gallery/default.aspx
    Live Support: http://cutesoft.net/live-support/default.aspx

  •  06-25-2007, 6:10 PM 31069 in reply to 30858

    Re: If user selection is null, editor1doc.selection.createRange().htmlText fails in Firefox?

    hey
    I use the ".execCommand('inserthtml', false, HTML)" command (atm only working on Firefox).
    But I guess the described problem  is occouring independent of with which command the html  is  inserted.
    Greetings


View as RSS news feed in XML