Getting already selected text
<p>By using following code I am getting the selected text's startindex and the selected text itself. I am storing them in a local database. I am changing the selected text background color to yellow.</p>

<pre><code>var mainDiv = document.getElementsByTagName("body")[0];
var sel = getSelectionCharOffsetsWithin(mainDiv);

var selectedText = window.getSelection();

location.href =  selectedText + '*' + sel.start; // this is to call iOS function.

var range = window.getSelection().getRangeAt(0);
var span = document.createElement("span");
span.style.backgroundColor = "yellow";
span.setAttribute("id", sel.start);
range.surroundContents(span);
</code></pre>

<p>Now, I am doing something else, and again I come back to same page. Here now I want to show previously selected text as highlighted.</p>
