admin管理员组文章数量:1327674
I want to highlight a selection of text on a web page. I have gone through this solution suggested by Tim Down which does half the job for me.
Now I need a way to highlight the text permanently. By permanently, I mean when I highlight some text, close the browser then re-open the page from local, the text should remain highlighted. Tim's solution keeps the text highlighted as long as I don't refresh or close the page. I guess I need to save the range's start and end offset somewhere so that next time I re-open the page I can highlight all the ranges again.
Edit: Sorry, forgot to mention that I am working on iPhone so I can keep an array of selections on local for a specific page. Any solution to store range for selection which can be nested or across multiple elements (e.g. div
/p
/span
)?
I want to highlight a selection of text on a web page. I have gone through this solution suggested by Tim Down which does half the job for me.
Now I need a way to highlight the text permanently. By permanently, I mean when I highlight some text, close the browser then re-open the page from local, the text should remain highlighted. Tim's solution keeps the text highlighted as long as I don't refresh or close the page. I guess I need to save the range's start and end offset somewhere so that next time I re-open the page I can highlight all the ranges again.
Edit: Sorry, forgot to mention that I am working on iPhone so I can keep an array of selections on local for a specific page. Any solution to store range for selection which can be nested or across multiple elements (e.g. div
/p
/span
)?
- 1 found a way to store range home.arcor.de/martin.honnen/javascript/storingSelection1.html – Farhan Commented Sep 4, 2010 at 9:54
4 Answers
Reset to default 4You need two things: some means of serializing the selection and some means of saving it. Cookies and/or local storage would do for the saving part. For the serializing/deserializing, I'd suggest creating some kind of path through the DOM using child node index at each level to specify the selection boundaries. See this answer to a similar question: Calculating text selection offsets in nest elements in Javascript
Edit: summary of the linked solution
The user's selection can be expressed as a Range
object. A Range has a start and end boundary, each expressed in terms of an offset within a node. What the the answer I linked to is suggesting is serializing each boundary as a path though the DOM that represents the node, coupled with the offset. For example, for the following document with the selection boundaries represented by |
:
<html><head><title>Foo</title></head><body>On|e <b>t|wo</b><div>
... you could represent the selection start boundary as "1/0:2"
, meaning offset 2 within the child node at position 0 of the child node at position 1 in the document root. Similarly the end boundary would be "1/1/0:1"
. You could represent the whole thing JSON as '{"start":"1/0:2",end:"1/1/0:1"}'
.
An option to save data locally would be to use cookies: http://www.w3schools./js/js_cookies.asp
or the HTML5 localStorage: http://people.w3/mike/localstorage.html
But the obvious drawback is that it's tied to the current browser and puter. If you want something more persistant, you'd want to use some kind of server side help. What are your requirements?
I guess you could then save start and end positions and then at page load re-create the range using
document.createRange
and then the methods setStart
and setEnd
https://developer.mozilla/en/DOM/document.createRange https://developer.mozilla/en/DOM/range.setStart
Here's a precious finding: MASHA (short for Mark & Share) is a JavaScript utility allowing you to mark interesting parts of web page content and share it http://mashajs./
It is also on GitHub https://github./SmartTeleMax/MaSha
Works even on Mobile Safari and IE
Rangy is a really nice cross-browser range and selection library written by Tim Down since he wrote his answer to this question. Its Serializer module wraps up the kind of selector Tim has described (e.g. "0/1/2:34") and adds a couple of helper functions for persisting the result to a cookie. You can easily ignore the helper functions though and persist somewhere else, such as local storage or a web service.
All in all, it's an excellent solution to the problem of persisting a selection across browser sessions.
本文标签: htmlHow can I persist a highlighted selection using JavascriptStack Overflow
版权声明:本文标题:html - How can I persist a highlighted selection using Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742223948a2435647.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论