Script creating multiple Divs

Asked

Viewed 110 times

0

I’m using javascript to update a div every 5 seconds, and every 5 seconds div updates, but is creating several div equal.

I have the following code:

session_start();
$url ="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$_SESSION['URL'] = $url;

echo("    <script>
$(document).ready(function(){
  $('#chatDiv').load('$url');

  var refreshId = setInterval(function(){
      $('#chatDiv').load('$url');}, 5000);
  $.ajaxSetup({ cache: false});
  });
</script>");
  • What returns from the URL you are loading into the div #chatDiv?

  • I’m getting the current page url

  • Just remembering that this HTTP_HOST is vulnerable, anyone can change it, since part of the header sent by the client. That is, changing this you will make a request to another site...

1 answer

0

I suggest using the $.get, and with that you can filter what comes back and take only the contents of div:

session_start();
$url ="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$_SESSION['URL'] = $url;

echo("    <script>
$(document).ready(function(){
   $.get( '$url', function(data){
      $('#chatDiv').html( $(data).filter('#chatDiv').html() );
   });

  var refreshId = setInterval(function(){
      $.get( '$url', function(data){ $('#chatDiv').html( $(data).filter('#chatDiv').html() ); })}, 5000);
  $.ajaxSetup({ cache: false});
  });
</script>");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.