Tuesday, March 28th, 2006

W3C Range for IE

Category: IE, JavaScript, Library

<p>Jorgen Horstink has started to hack away on a W3C Range implementation for Internet Explorer. An example of its usage is:

javascript
< view plain text >
  1. <script src="function.prototype.js" type="text/javascript" />
  2. <script src="range.js" type="text/javascript" />
  3. <script type="text/javascript">
  4.   window.onload = function () {
  5.     var sel   = new Selection();
  6.     var range = new Range();
  7.  
  8.     range.setStart(someTextNode, 4);
  9.     range.setEnd(someElementNode, 2);
  10.     sel.addRange(range);
  11.   }
  12. </script>

The result will be exactly the same for Internet Explorer as for browsers which implement W3c Range correctly.

Also the decomposition of an Internet Explorer TextRange to a W3c Range is possible. By this, you are allowed to retrieve the startContainer and startOffset properties of the current selection. pretty neat!

Update: Jorgen’s original post on this is here. Sorry about missing that the first time, Jorgen.

Related Content:

Posted by Dion Almaer at 8:49 am
7 Comments

+++--
3.9 rating from 40 votes

7 Comments »

Comments feed TrackBack URI

No link to the actual post?

Comment by Martin Bialasinski — March 28, 2006

I believethis is the link to the original post everyone is looking for: http://jorgenhorstink.nl/2006/03/11/w3c-range-in-internet-explorer/

Comment by Richard Soares — March 28, 2006

Martin: We had a plugin set up incorrectly. Should be fixed now. Sorry about that.

Comment by Rob Sanheim — March 28, 2006

Looks funny :)

Comment by Erde — April 27, 2006

Hi, i’m working on a rich text editor based on jquery and i want to share with you how i made surroundContents and insertNode to work:
considering that this will refer to the current range object.

insertNode: function(node){
if( this.insertNode)
return this.insertNode(node);
var tmpParent = window.document.createElement('div');
tmpParent.appendChild(node);
this.collapse();
this.pasteHtml(tmpParent.innerHTML);
}
surroundContents: function(node){
if( this.surroundContents )
return this.surroundContents(node);
var tmpParent = window.document.createElement('div');
tmpParent.appendChild(node);
node.innerHTML += r.htmlText;
r.pasteHTML(tmpParent.innerHTML);
}

hope this’ll help to improve your code, thanks for your sharing,

malko

Comment by malko — January 21, 2008

Bad regex!

Comment by eyelidlessness — November 4, 2008

Thanks admin

Comment by sikis — April 17, 2009

Leave a comment

You must be logged in to post a comment.