随便在CSDN外包频道上逛,找到一个这样的东东:
Web 页面上的选取区域的记录和恢复
看了2分钟才算明白过来是什么意思,其实是选择当前页面的某些内容,将这些选择保存起来.
地址: http://home.arcor.de/martin.honnen/javascript/storingSelection1.html
代码先记下,在firefox中可用,IE需要修改
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
- "http://www.w3.org/TR/html4/strict.dtd">
- <html lang="en">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>storing Mozilla selection as XPath, offset pair</title>
- <script type="text/javascript">
- function setCookie (cookieName, cookieValue, expires, path, domain, secure) {
- document.cookie =
- escape(cookieName) + '=' + escape(cookieValue)
- + (expires ? '; EXPIRES=' + expires.toGMTString() : '')
- + (path ? '; PATH=' + path : '')
- + (domain ? '; DOMAIN=' + domain : '')
- + (secure ? '; SECURE' : '');
- }
- function getCookie (cookieName) {
- var cookieValue = null;
- var posName = document.cookie.indexOf(escape(cookieName) + '=');
- if (posName != -1) {
- var posValue = posName + (escape(cookieName) + '=').length;
- var endPos = document.cookie.indexOf(';', posValue);
- if (endPos != -1)
- cookieValue = unescape(document.cookie.substring(posValue, endPos));
- else
- cookieValue = unescape(document.cookie.substring(posValue));
- }
- return cookieValue;
- }
- </script>
- <script type="text/javascript">
- function makeXPath (node, currentPath) {
- /* this should suffice in HTML documents for selectable nodes, XML with namespaces needs more code */
- currentPathcurrentPath = currentPath || '';
- switch (node.nodeType) {
- case 3:
- case 4:
- return makeXPath(node.parentNode, 'text()[' + (document.evaluate('preceding-sibling::text()', node, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength + 1) + ']');
- case 1:
- return makeXPath(node.parentNode, node.nodeName + '[' + (document.evaluate('preceding-sibling::' + node.nodeName, node, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength + 1) + ']' + (currentPath ? '/' + currentPath : ''));
- case 9:
- return '/' + currentPath;
- default:
- return '';
- }
- }
- function storeSelection () {
- if (typeof window.getSelection != 'undefined') {
- var selection = window.getSelection();
- var range = selection.getRangeAt(0);
- if (range != null) {
- setCookie('sel', makeXPath(range.startContainer) + '|' + range.startOffset + '|' + makeXPath(range.endContainer) + '|' + range.endOffset);
- }
- }
- }
- function restoreSelection () {
- var selectionDetails = getCookie('sel');
- if (selectionDetails != null) {
- selectionDetailsselectionDetails = selectionDetails.split(//|/g);
- if (typeof window.getSelection != 'undefined') {
- var selection = window.getSelection();
- selection.removeAllRanges();
- var range = document.createRange();
- range.setStart(document.evaluate(selectionDetails[0], document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue, Number(selectionDetails[1]));
- range.setEnd(document.evaluate(selectionDetails[2], document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue, Number(selectionDetails[3]));
- selection.addRange(range);
- }
- }
- }
- window.onload = restoreSelection;
- </script>
- </head>
- <body>
- <h1>storing Mozilla selection as XPath, offset pair</h1>
- <div>
- <input type="button" value="mouse here to store current selection"
- onmouseover="storeSelection();">
- </div>
- <p>Kibology for all. All for Kibology. <img src="kiboInside.gif" alt="Kibo inside"></p>
- </body>
- </html>