I looking-for solution how to extract url from selected text using window.getSelection
and document.selection
.
Text to select look that:
<p>Lorem Ipsum is <a href="http://example.com">simply <b>dummy</b> text</a> of the printing and typesetting industry.</p>
Selected text (selected by user) to extract link:
Option 1 (include text and text between a tags):
Ipsum is simply dummy text of
Option 2 (select text and fragment of link):
Ipsum is simply
The function should be return http://example.com
It's hard to write up for cross-browser function. See How to bind a handler to a selection change on window?.
We should capture some events like
mousedown
,mouseup
,touchstart
,touchend
. The combination of these event may be fine.Next is the
getSelectedHTML()
usingwindow.getSelection
ordocument.selection
.This code should be checked especially with old browsers.
Here is the sample fiddle: http://jsfiddle.net/tokkonoPapa/CQ63a/