javascript - How to captured Selected Text Range in iOS after text selection expansion -
i'm working on web app allows user select text, click button, , save highlighted text. works great in desktop browsers, (chrome example), in ios i'm having issues native text selection, user can change selected text.
here jsfiddle showing issue (issue exists in ios): http://jsfiddle.net/jasonmore/gwzfb/
user starts text selection
user expands text selection, , clicks "show selected text above"
only first selected word "the" shows up, though want "the path of righteous man"
1 begin 
2 select text , hit button 
3 "the" 
here js using:
$(function() { $('#actionbutton').click(function() { $('#result').text(selectedrange.tostring()); }); $('#slipsum').on('mouseup touchend','p', function() { getselectedrange(); }); }); var selectedrange = null; var getselectedrange = function() { if (window.getselection) { selectedrange = window.getselection().getrangeat(0); } else { selectedrange = document.getselection().getrangeat(0); } }; html:
<h3>selected text:</h3> <p id="result"> </p> <br/> <p> <input type="button" id="actionbutton" value="show selected text above" /> </p> <!-- start slipsum code --> <div id="slipsum"> <h1>is dead, yes or no?</h1> <p>do see teletubbies in here? see slender plastic tag clipped shirt name printed on it? see little asian child blank expression on face sitting outside on mechanical helicopter shakes when put quarters in it? no? well, that's see @ toy store. , must think you're in toy store, because you're here shopping infant named jeb. </p> <h1>so, cold?</h1> <p>the path of righteous man beset on sides iniquities of selfish , tyranny of evil men. blessed who, in name of charity , will, shepherds weak through valley of darkness, brother's keeper , finder of lost children. , strike down upon thee great vengeance , furious anger attempt poison , destroy brothers. , know name lord when lay vengeance upon thee. </p> <h1>i'm serious heart attack</h1> <p>do see teletubbies in here? see slender plastic tag clipped shirt name printed on it? see little asian child blank expression on face sitting outside on mechanical helicopter shakes when put quarters in it? no? well, that's see @ toy store. , must think you're in toy store, because you're here shopping infant named jeb. </p> <h1>is dead, yes or no?</h1> <p>like you, used think world great place lived same standards did, kid nail showed me living in world, world chaos rules not order, world righteousness not rewarded. that's cesar's world, , if you're not willing play rules, you're gonna have pay price. </p> <h1>is dead, yes or no?</h1> <p>your bones don't break, mine do. that's clear. cells react bacteria , viruses differently mine. don't sick, do. that's clear. reason, , react exact same way water. swallow fast, choke. in our lungs, drown. unreal may seem, connected, , i. we're on same curve, on opposite ends. </p> </div> <!-- please not remove line --> <div style="display:none;"> <a href="http://slipsum.com">lorem ipsum</a></div> <!-- end slipsum code -->
to stumbles upon issue in future, here resolution:
http://jsfiddle.net/jasonmore/gwzfb/
$(function() { $('#actionbutton').click(function() { if (selectedrange) { $('#result').text(selectedrange.tostring()); clearinterval(timer); } }); timer = setinterval(getselectedrange, 150); }); var timer = null; var selectedrange = null; var getselectedrange = function() { try { if (window.getselection) { selectedrange = window.getselection().getrangeat(0); } else { selectedrange = document.getselection().getrangeat(0); } } catch (err) { } };
Comments
Post a Comment