22439

How To Select A Range in an Iframe With “Start” and “End” in Firefox like “selectionStart” from inpu

for Internet Explorer, I have the current code to select a range in an iframe with desingMode setting to on:

    var Range = window.document.selection.createRange();
    var obj = { start: 3</b>, end : 6</b>}
    Range.collapse( true );
    Range.moveStart( 'character', obj.start );
    Range.moveEnd( 'character', obj.end - obj.start );
    Range.select();</pre>

Most useful if I want select only one piece of a string by only 2 parameters. Start and End ( for input elements exists the properties selectionStart and selectionEnd ).

When this code is executed, for example, on a string "Hello World", it highlight only the piece of string llo Wo</b>, or something like that. The problem is that Firefox Dom do not support the method moveStart or moveEnd, but only range.setStart and range.setEnd which request only a node and an offset as arguments.

So is possible to virtualizing the moveStart and the moveEnd methods in Firefox? Thanks.

Answer1:

This works for me:

var iframeElement = ...; // the DOM element for the iframe; var contentDoc = iframeElement.contentDocument; var range = contentDoc.createRange(); range.setStart(contentDoc.body.firstChild, 3); range.setEnd(contentDoc.body.firstChild, 6); var selection = iframeElement.contentWindow.getSelection(); selection.removeAllRanges(); selection.addRange(range);

You need to call removeAllRanges() in Firefox or else you can end up with multiple selections.

Recommend

  • IHTMLTxtRange.pastHTML limit?
  • Filter records in gridview
  • regex, multiline extract in R
  • dc.js: Reducing rows in data table
  • Extract data between rows r
  • Rails 4.1 environment variables not reloading
  • CSS: How to fix overlapping divs
  • Does Context/Scoping of a SQLAlchemy Session Require Non-Automatic Object/Attribute Expiration?
  • phpmailer - How to verify a sent email arrived at its destination
  • Is WITH the replacement for a #TEMP table?
  • Insert records if not exist SQL Server 2005
  • Using HTML/CSS for UI in XNA?
  • How to configure Cygnus in relation to Orion and Cosmos
  • RavenDB indexing errors
  • C function strchr - How to calculate the position of the character?
  • How to repeat sections of a SQL query across UNIONs? (DRY in SQL)
  • Pre-populated SQLite Database not reading properly in Android Studio
  • C++ cout and enum representations
  • What command do i need to pass in SabreCommandLLSRQ to get current price of PNR?
  • Cannot save model when using ember render helper
  • Trying to get the char code of ENTER key
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Groovy: Unexpected token “:”
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • How to create a file in java without a extension
  • Q promise. Difference between .when and .then
  • MySQL WHERE-condition in procedure ignored
  • Matplotlib draw Spline from multiple points
  • AT Commands to Send SMS not working in Windows 8.1
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • python regex in pyparsing
  • C# - Getting references of reference
  • Android Google Maps API OnLocationChanged only called once
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • How can I use threading to 'tick' a timer to be accessed by other threads?