5

When you tap and hold on a link inside a UIWebView (or mobile safari), it highlights the link in a gray text box.

You can control the styling of this with the webkit-tap-highlight css property.

What I'd like to know is if it is possible to either: A. Listen to an event for when something is highlighted. B. At a given time, find any elements that are currently highlighted.

Is this currently possible?

Nate Weiner
  • 805
  • 2
  • 9
  • 15
  • Can you use [this](https://developer.mozilla.org/en/DOM/window.getSelection)? (Sorry, never developed specically for mobile safari...) – Tomas Reimers Nov 28 '11 at 02:42
  • Was 'this' suppose to be a link? – Nate Weiner Nov 28 '11 at 19:19
  • ['this'](https://developer.mozilla.org/en/DOM/window.getSelection) is a link... – Tomas Reimers Nov 28 '11 at 19:55
  • haha, sorry, totally missed that. Yea I tried window.getSelection it doesn't seem to grab highlighted text. Only when the text is actually selected. – Nate Weiner Nov 30 '11 at 22:27
  • Out of curiosity, why can you just [do this](http://stackoverflow.com/a/8092444/781199) (<- HINT THAT IS A LINK ;) ) and then bind the touch start touch end events to highlight the object and call whatever functions you originally wanted called... (i.e. basically rewrite the whole process in js)? – Tomas Reimers Dec 01 '11 at 01:08
  • That could work but ideally I wouldn't have to modify the page that is being displayed. Thanks for that though. – Nate Weiner Dec 02 '11 at 19:54

2 Answers2

0

Although the iPhone is a touch device, the mouseover event will be triggered when the element is highlighted. So, you can use javascript to listen the mouseover event.

Robert Karl
  • 7,598
  • 6
  • 38
  • 61
0

Looks like this guy figured it out using window.getSelection() and text.anchorNode.textContent.substr(text.anchorOffset, text.focusOffset - text.anchorOffset):

http://zaldzbugz.wordpress.com/2010/05/31/how-to-get-the-highlighted-text-in-uiwebview/

This article was also chosen as a working answer in another StackOverflow post.

Community
  • 1
  • 1
JD Smith
  • 1,764
  • 15
  • 17