Click here to Skip to main content
15,914,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am interested in saving text when user selects any text anywhere on a web page that text has to be highlighted and has to save that text as a string in DataBase.
when same user saw same page next time text has to be highlighted as done previously by him.
If anyone knows a simple elegant way to do this I would really appreciate it, but I'll take any solution at this point. Even if you can point me in the right directions that would be appreciated. Thanks in advance.
Posted
Updated 22-Sep-11 1:57am
v3

Hi,

I tried this code it works fine.Try once

If you use Jquery you can pass capture text to server.


HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script language ="javascript" >
 
        document.onselectstart = df;
        document.onmouseup = dfu;

        function dfu(event) {
            document.getElementById("showtextid").innerHTML = "selection ended the selected text is   :   " + document.selection.createRange().text+"";
        }
        function df(event) {
            document.getElementById("showtextid").innerHTML = "selection started" ;

        }
      
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <div id="showtextid"></div><br />
       this is just for testing

       I am interested in saving text when user selects any text anywhere on a web page that text has to be highlighted and has to save that text as a string in C#.
when same user saw same page next time text has to be highlighted as done previously by him.
If anyone knows a simple elegant way to do this I would really appreciate it, but I'll take any solution at this point. Even if you can point me in the right directions that would be appreciated. Thanks in advance.
    </div>
    </form>
</body>
</html>

If you are using Jquery then no problem.

I hope you understood what I did

Use $.post() to post selected data to server and save in database .



All the Best.
 
Share this answer
 
Comments
rkthiyagarajan 22-Sep-11 11:15am    
Hi Murali, I really proud you ..and your way of explanation is too good.
Thanks for your reply but it will not work other than IE .

I m tried with this code

C#
function highlight() {
           var range, sel;
           // IE case
           if (document.selection && document.selection.createRange) {
               range = document.selection.createRange();
               //range.execCommand("Bold", false, null);
               range.execCommand("BackColor", "false", "yellow");
               txt = document.selection.createRange().text;
           } else if (window.getSelection) {
               // Non-IE
               sel = window.getSelection();
               if (sel.rangeCount && sel.getRangeAt) {
                   range = sel.getRangeAt(0);
                   alert(range);
               }
               document.designMode = "on";
               if (range) {
                   sel.removeAllRanges();
                   sel.addRange(range);
                   alert(range);
               }
               // document.execCommand("Bold", false, null);
               document.execCommand("BackColor", "false", "yellow");
               document.designMode = "ofx  f";
           }
       }
   </script>

but using this code i m getting problem in firefox but it works fine in IE,Chrome and also i have show highlight the text when same user open same page next time For that i have to store text in my database.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900