1

Problem: Selection#anchorOffset, Selection#baseOffset & Selection#focusOffset from time to time returns wrong values.

Expected behavior: Make double click on the second word in code snippet (feature). You should see alert 'anchor: feature'.

Question: How to get a correct value?

JSFiddle Demo: http://jsfiddle.net/v2mmabzm/5/

Demo:

$(document).ready(function()
{
    var p = $('p.text-container')
    p.css({ cursor: 'pointer' });
    p.dblclick(function(e) {
        var html = $(e.target).html();
        var range = window.getSelection() || document.getSelection() || document.selection.createRange(); 
        var selectedWord = $.trim(range.toString());            
        debugger;
        var anchorOffset = range.anchorOffset;
        var baseOffset = range.baseOffset;
        var focusOffset = range.focusOffset;        
        alert('anchor: ' + html.substring(anchorOffset, anchorOffset + selectedWord.length));
        alert('base: '   + html.substring(baseOffset,   baseOffset   + selectedWord.length));
        alert('focus: '  + html.substring(focusOffset,  focusOffset  + selectedWord.length));
    });    
});
span.touched {
    font-weight: bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<p class="text-container">
The future of manned space exploration and development of space depends critically on the creation of a dramatically more proficient propulsion architecture for in-space transportation. A very persuasive reason for investigating the applicability of nuclear power in rockets is the vast energy density gain of nuclear fuel when compared to chemical combustion energy. Current nuclear fusion efforts have focused on the generation of electric grid power and are wholly inappropriate for space transportation as the application of a reactor based fusion-electric system creates a colossal mass and heat rejection problem for space application. The Fusion Driven rocket (FDR) represents a revolutionary approach to fusion propulsion where the power source releases its energy directly into the propellant, not requiring conversion to electricity. It employs a solid lithium propellant that requires no significant tankage mass. The propellant is rapidly heated and accelerated to high exhaust velocity (&gt; 30 km/s), while having no significant physical interaction with the spacecraft thereby avoiding damage to the rocket and limiting both the thermal heat load and radiator mass. In addition, it is believed that the FDR can be realized with little extrapolation from currently existing technology, at high specific power (~ 1 kW/kg), at a reasonable mass scale (&lt;100 mt), and therefore cost. If realized, it would not only enable manned interplanetary space travel, it would allow it to become common place. The key to achieving all this stems from research at MSNW on the magnetically driven implosion of metal foils onto a magnetized plasma target to obtain fusion conditions. A logical extension of this work leads to a method that utilizes these metal shells (or liners) to not only achieve fusion conditions, but to serve as the propellant as well. Several low-mass, magnetically-driven metal liners are inductively driven to converge radially and axially and form a thick blanket surrounding the target plasmoid and compress the plasmoid to fusion conditions. Virtually all of the radiant, neutron and particle energy from the plasma is absorbed by the encapsulating, metal blanket thereby isolating the spacecraft from the fusion process and eliminating the need for large radiator mass. This energy, in addition to the intense Ohmic heating at peak magnetic field compression, is adequate to vaporize and ionize the metal blanket. The expansion of this hot, ionized metal propellant through a magnetically insulated nozzle produces high thrust at the optimal Isp. The energy from the fusion process, is thus utilized at very high efficiency. Expanding on the results from the phase I effort, phase II will focus on achieving three key criteria for the Fusion Driven Rocket to move forward for technological development:
    </p>
</body>

8
  • 1
    Can you give an example of the desired behavior - something that would constitute a correct value? Commented Nov 14, 2014 at 15:32
  • is it due to you using 'doubleclick' rather than 'click' or 'onmouseup'? when you doubleclick it actually selects the word you click on and therefore changes the selection you have made. Like Alex says, could do with understanding what you are trying to achieve Commented Nov 14, 2014 at 15:34
  • @alex sure. Just run code snippet and double click the second word (feature). You should see anchor alert selected word Commented Nov 14, 2014 at 15:34
  • @kipty I want to alert the word I've doubleclicked Commented Nov 14, 2014 at 15:34
  • Is anchor, base, or focus returning the incorrect value, and can you describe what would constitute a correct value for each of these three? anchor does alert the word I've doubleclicked - at least in Firefox. Commented Nov 14, 2014 at 15:41

1 Answer 1

2

Just change

var html = p.text();
Sign up to request clarification or add additional context in comments.

6 Comments

I accepted your answer, but it still doesn't work in some cases. Steps to reproduce: 1) Go to jsfiddle.net/v2mmabzm/13 2) Find The propellant is rapidly... 3) dbclick on propellant 4) dbclick on The 5) dbclick on propellant again [actual result]: anchorOffset == 1
I can explain you more, if you send me message (see my profile)
your code, which wrap selected text in span make significant change - it split your content of p on three node - #text, <span> and #text again. anchorNode property belongs to node, where selected was started. It is third node and anchorOffset in this case - 1, it's right behaviour.
You can check my words if you get childNodes collection of p-tag or call jquery method .contents().
I understand. But why $(e.target) till points to the whole paragraph? How to know html of which childNode I should replace?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.