4

A user selects text and it replaces with 'a' tag id='NewLink', when he'll has finished with description for a word or phrases the server will get innerHTML of content div 'StartId'. I need to overlook that a user doesn't change the html code on a client side in browser. How I can do it?

  function GetSelectedText () {
   var sel = window.getSelection();  
            
   if( sel.toString().length != 0 && sel.getRangeAt) {
   $('#NewLink').replaceWith($('#NewLink').html());

     var rng, se, err, errt, patt;
     errt = 0;
     try {
    
     se=$('<span style="background-color: #3366CC;" id="NewLink">')[0];

        rng=sel.getRangeAt(sel.rangeCount-1);
        rng.surroundContents(se);
        rng.selectNode(document.getElementById("NewLink"));
         } catch (err) { errt = 1;}
    
        if(errt != 1)  
        {
        patt=new RegExp("</?a.*?>","g");
        if (patt.test($("#NewLink").html()) ||$("#NewLink").closest("a").length||!$("#NewLink").text().replace(/\s/g,"").length) 
        {$("#NewLink").replaceWith($("#NewLink").html()); }
        else { rng.collapse(!0);}
        }
    }
    var StartId = document.getElementById("StartId").innerHTML;
    $.post('site.com', {htmlInner:StartId}, function(data) {
    alert(data);
    });
      
    } 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="StartId" onmouseup="GetSelectedText ()">
Carnotaurus, a large theropod dinosaur, lived during the Late Cretaceous period. Known from a single well-preserved skeleton found in Argentina, it is a member of the Abelisauridae family<a href="#">and one of the best-understood theropods from the Southern Hemisphere. <br>Carnotaurus (derived from Latin for "meat-eating bull") <br> had thick horns above the eyes, and a very deep skull on a muscular neck.</a> <br>It was a lightly built, bipedal predator, 8 to 9 m (26.2 to 29.5 ft) long, weighing at least 1.35 metric tons (1.49 short tons). It had small, vestigial forelimbs and long and slender hindlimbs. Preserved skin impressions show a mosaic of small scales interrupted by large bumps that lined the sides of the animal. </div>
<div id="EndId"></div>
Дмитрий
  • 83
  • 1
  • 10
  • The user can change the HTML and still manipulate the request body so it will appear as the test wasn't really changed by the client. May I ask why do you need this? – Alon Eitan Apr 23 '17 at 15:58
  • @Alon Eitan, I create website with similar function like here https://genius.com/11715748, I need to get a new html to replace it in database. – Дмитрий Apr 23 '17 at 16:01
  • Will [this answer](http://stackoverflow.com/a/5379408/754119) help you with the selection part? Because it look like your code already sending the actual innerHTML to the server – Alon Eitan Apr 23 '17 at 16:05
  • @Alon Eitan, If I will select only letter 'a' how will I get which one in text it is? It doesn't help me ( – Дмитрий Apr 23 '17 at 16:10
  • I think I need to duplicate the StartId by the load page and change into it data, I'd tried to set setStart and setEnd and replace there vainly. – Дмитрий Apr 23 '17 at 16:32

1 Answers1

0

change your $.post code to this:

$.post('site.com', '{"htmlInner":"' + StartId + '"}', function(data) {
       alert(data);
    });          
} 

your parameter not properly serialized.

if your htmlInner is an integer, you can change the parameter to this:

'{"htmlInner": '+ StartId + '}'

Jeric Cruz
  • 1,899
  • 1
  • 14
  • 29
  • _if your htmlInner is an integer,_ / Stringifying the object?? What are you talking about. This answer doesn't look anything like a possible solution - . Hope your right and I'm wrong so it will help the OP... – Alon Eitan Apr 23 '17 at 16:19