javascript - Prevent automatic "scrolling up" with jquery ajax $.load -


i trying build ajax chat jquery , php. working fine except scrolling. basically, i've set-up time-out when inner-content of chat automatically reload makes chat box scroll way if scrolling. here's code without many li (messages) (they loaded php):

http://jsfiddle.net/m72jj/1/

and here's live version can see (the scrolling doesn't happen in jsfiddle):

http://alfie.co.nf/templates/sandbox/php/ajax-chat/

here's jquery:

var form = $('form');  form.submit(function(event){     event.preventdefault();     var username = form.find('input[name=user]').val(),         messform = form.find('input[name=message]')         mess     = messform.val(),         url      = form.attr('action');      var posting = $.post(url, {message : mess , user : username});     posting.done(function(data){         var content = $(data).find('.chatbox');         $('.chatbox').empty().append(content).fadein();         messform.val("").fadein();     }); })  function checknewmessages(){     $('.chatbox').load(" .chatbox"); }  setinterval(checknewmessages, 500); 

update oh well, saw directly point load onto chatbox itself, replace completely. loose scroll position sure.

try add container around .chatbox , change styles of .chatbox container around (and don't name .chatbox).

here's example of meant (compared first version of jsfiddle):

http://jsfiddle.net/m72jj/2/

hope helps.

original post seems problem lies in jquery load method.

i created small example whole content reset, not using ajax nor $.load:

http://jsfiddle.net/aybsq/

html:

<div id="outercontainer">     <div id="contentcontainer">     </div> </div> 

javascript:

jquery(document).ready(function() {     var linecount = 0;     var outercontainer = $('#outercontainer');      var contentcontainer = $('#contentcontainer');      var updatecontent = function () {         contentcontainer.empty();         (var index = 0; index < 50; index++){             linecount++;             contentcontainer.html(contentcontainer.html() + '<p>' + linecount + ': 1 chat content line</p>');            }     }      window.reloadinterval = setinterval(function() { updatecontent(); }, 1000); }); 

perhaps should create ajax endpoint in php script messages rendered, use standard $.ajax call , put resulting html using $(element).html().

anyway, in opinion change message-loading plain html json, , cache chat messages on client , send new messages server. reduce amount of data transferred. achieved using framework knockoutjs fill observablearray , render array using knockout. longer story tell ;)

hope helps.

best regards, chris


Comments

Popular posts from this blog

Why can rails not find a route created by a helper? -

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -