Problem of GetSelection() in IE

Last post 11-06-2008, 6:59 AM by AndyFel. 7 replies.
Sort Posts: Previous Next
  •  10-22-2008, 2:30 PM 45039

    Problem of GetSelection() in IE

    i want to run this code in IE, it is working fine in Firefox.
     
    Code:- 
    var e1 = document.getElementById('<% =Editor1.ClientID%>');
     
    var selectedDiv = e1.GetSelection();
     
    selectedDiv.anchorNode.parentNode.innerHTML = '<b>' + selectedDiv.anchorNode.parentNode.innerHTML + '</b>'
     
     
     
    Somehow in IE, selectedDiv.anchorNode is keep coming as undefined. Please provide me the alternative code.
     
    I tried the sample code given at http://cutesoft.net/forums/thread/37318.aspx, but it didn't work.

    Thanks
    Karan

    Karan Singh
    (Web Developer)
  •  10-25-2008, 8:10 AM 45092 in reply to 45039

    Re: Problem of GetSelection() in IE

  •  11-05-2008, 4:18 AM 45404 in reply to 45092

    Re: Problem of GetSelection() in IE

    Hi Adam,
     
    Please give me an alternative solution to this major problem. The solution u gave is not working in IE.
     
     

    Karan Singh
    (Web Developer)
  •  11-05-2008, 11:08 AM 45429 in reply to 45404

    Re: Problem of GetSelection() in IE

    Can you check my answer in the following thread?
     
     
    If you think it doesn't work for you, please provide some details.

    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

  •  11-06-2008, 1:46 AM 45447 in reply to 45429

    Re: Problem of GetSelection() in IE

    Hi Adam,
     
    I tried the code in IE. Please have a look and give a suggestion.
     
     
    Code:-
     
    var e1 = document.getElementById('<% =Editor1.ClientID%>');
    var editdoc = e1.GetDocument();
    var editwin = e1.GetWindow();
    var rng=null,html="";

     

    if (document.selection && document.selection.createRange){
    rng=editdoc.selection.createRange();
    html=rng.htmlText||"";      // htmlText is coming out to be blank.And this is the problem in IE.
    alert(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());
    }

    }

     

    alert(html);

     


    Karan Singh
    (Web Developer)
  •  11-06-2008, 4:03 AM 45452 in reply to 45447

    Re: Problem of GetSelection() in IE

    Hi Maximus
     
    We have the following code which we have been using to select text. This was on the forums a couple of years ago, I have tried the new example above and this seemed to work fine for me (altho I only did a quick test). Anyway hope the below helps.

     

    function getLinkHTML()
    {
      // get the cute editor instance
      var editor1 = document.getElementById('<%=Editor1.ClientID%>');
      // get the selected text or image etc..
      var editselection=editor1.GetSelection(); 
      var ret = "";
      // if firefox
      if(editselection.rangeCount > 0)
      {
        var range=editselection.getRangeAt(0);
        var clonedSelection = range.cloneContents();
        var div = document.createElement('div');
        div.appendChild(clonedSelection);
        ret = div.innerHTML;
      }
      // if IE
      else
      {
        var range=editselection.createRange();
        // If the selection is text
        if(editselection.type=='Text')
        {
          ret = range.htmlText;
        }
        if(editselection.type=='Control')
        {
          if(range.length==1)
          {
            var element=range.item(0);
            // If the selection is a image
            if(element.tagName=='IMG')
            {
              //gets all the html of the image tag to use in the link
              src = element.getAttribute("outerHTML") + '' ;
              ret = src;
            }
          }
        }
      }

      return ret;
    }

     

  •  11-06-2008, 5:40 AM 45458 in reply to 45452

    Re: Problem of GetSelection() in IE

    Thanks for your help. The solution is working in IE but not in the firefox. I wish to get the whole outerhtml in firefox similar to the way it is done in IE.
     
    Please suggest.
     
    // if firefox
      if(editselection.rangeCount > 0)
      {
        var range=editselection.getRangeAt(0);
        var clonedSelection = range.cloneContents();
        var div = document.createElement('div');
        div.appendChild(clonedSelection);
        ret = div.innerHTML;
      }
     

    Karan Singh
    (Web Developer)
  •  11-06-2008, 6:59 AM 45460 in reply to 45458

    Re: Problem of GetSelection() in IE

    Ermmm I thought you started off this thread saying that you had it working in firefox and not IE.
    If so cant you take the working part in firefox and replace the above with it.
     
    The example I gave, was a suggestion that may help you get to your goal, basically we use it to get the selected text, but if you do select an image it does return the whole <img ..... /> tag just as if you requested outerHTML. 
     
    Anyway if none of the above suggestions help, then i guess its back to the cutesoft support guys for help, i am just a cutesoft user.
View as RSS news feed in XML