tags:

views:

51

answers:

2

Hi. I make this code that, after 9 seconds, it call an ajax function, and print the result elaborated from the server to the client.

This is the JS code :

function changeSponsor() {
    $.ajax({
        type: 'POST',
        cache: false,
        url: './auth/ajax.php',
        data: 'id=changespon',
        success: function(msg) {
            $('.menusponsor').hide().fadeIn(1000).html(msg);
        }
    });
}

$(document).ready(function() {
    x=window.setInterval("changeSponsor()", 9000);
});

the result is printed on a div at the top of the page. when the result is printed to the client (after, as said, 9 seconds), and I am at the bottom of the page, the page go automatically at the top. I don't want this.

You can see an exemple at this link : open this page, go to the bottom (is not so long this page) and after few seconds (9). You will se the page scroll at the top.

How can resolve this problem? Cheers

+1  A: 

Can you tell us a little more about what happens when the page jumps to the top? Are you calling this function somewhere other than this setInterval?

What does your html look like? If you're replacing a huge portion of the page, it's possible that, for a split second, the page is very short, putting you at the top of the page.

treeface
this function just start (as you can see in the document.ready function) when the page is loaded. I have nothing that call it, just the page itself. The page go to the top when it get the data from the server, after (naturally) the ajax call. Is it more clear now? :)
markzzz
@markzzz No not really. You're just describing what you see, but you're not showing me your html. The short answer is: there's nothing wrong with the code you posted (even if that `.hide().fadeIn(1000).html(msg)` is a bit nebulous), so it has to be somewhere else.
treeface
ok! i've added a link, so you can see the whole code :)
markzzz
Grazie mille. Lo guardero' fra poco.
treeface
Thanks man. Nice Italian :)
markzzz
@markzzz Indeed :-). OK, so I'm still a bit unsure why this is happening. I made a mock up of this problem [here](http://jsfiddle.net/PEjrt/4/), but as you can see, I can't replicate it. Try doing something for me: change your code to read `.hide().html(msg).fadeIn(1000);`. Let me know what happens. Also...why do you use a `.` before your urls?
treeface
ok! Ive swapped the 2 functions, but nothing happens :) I put the dot before my urls for the relative/absolute path. My website doesnt change root during the time, so i cant find differences between put the dot or not put the dot :)
markzzz
@markzzz Maybe it's because the Italian internet is just trying to annoy us? :]...just kidding. Still looking around to try to solve this..
treeface
@markzzz Can you try getting rid of the `hide()` and `fadeIn` altogether, replacing it with only the `html(msg)` part?
treeface
Whoa...I just realized that this only seems to happen on your home page and several other pages. [This page](http://www.cabigolino.it/index.php?general=viewscores) doesn't seem to have the problem at all. Can you go through the pages where it does happen and the ones where it doesn't and maybe find things that are different?
treeface
@markzzz Also, if I were you, I'd just use a `<table>` to display tabular data. Your markup on that risultati page is kind of rough...and probably not easy at all to manipulate.
treeface
Yeah i know my english is crap, and i ask sorry! Back to the topic, this happen to all page, but in some different ways (for example in the Risultati zona the page scroll a bit up and down, or at least it happen so with chrome). Usually I don't use table, i find it obsolete. Im happy using div! thanks to the tip anyway :)
markzzz
@markzzz No worries. Sorry I can't help more, but I've run out of ideas. Keep poking around and you'll find it. Alle tabelle...I know where you probably got this idea, but trust me...tables are not obsolete. For about a decade there, tables were misused for the purposes of creating a layout (e.g. people would use it to vertically center things). In this sense, tables are "obsolete", but using divs for displaying tabular data is just as big of a mistake. It makes your data more difficult to maintain for developers and more difficult for search engines and data gatherers to read.
treeface
@markzzz meaningful html is always good, tables are also meaningful for tabular data, if you only have a list then your good to go.
YuriKolovsky
+2  A: 

It doesn't look like anything that you mention would move the page up, it must be something else?

I found that the page moves up normally when you do something to the url, like adding a hash (#)? are you adding a hash or altering the url in any way?

solution:

oh the problem is with your html, you need to hide the child of menusponsor and not the container itself.

try this

function changeSponsor() {
    $.ajax({
        type: 'POST',
        cache: false,
        url: './auth/ajax.php',
        data: 'id=changespon',
        success: function(msg) {
            $('.menusponsor').find('div').hide().fadeIn(1000).html(msg);
        }
    });
}
YuriKolovsky
no, i don't alter the URL. I tried to add the return false; but it doesnt change! can i post a link? so you can see :)
markzzz
I think i know how to fix your problem
YuriKolovsky
I have altered my solution to work :)
YuriKolovsky
Ohhh yeah! In fact this works, but i need to solve another problem right now, maybe using Json, because i can't send an array trought Jquery (or at least i think). But why if i hide the container the page move to the top? however i manage a div...! Anyway, Thanks for your effort :)
markzzz
no problem :) (if my solution solves your problem then mark my answer as accepted) as for the array, you can either serialize the array, or send it as json which is much better IMO, jquery ajax has a optional accepttype variable that accepts a value of 'json'. here is a shortcut: $.post(url,vars,function(){},'json')
YuriKolovsky
if hide the container, you remove the container from the page flow, which reduces the page size, and scrolls you up, so doing it on the children is much easier.
YuriKolovsky
Yeah it resolve my problem, but i still don't understand why it resolve the problem. In fact, if the pagesize is reduced, i might see a small scroll up. This don't happened : just the whole page go to the top. Its different (if you understand what i mean hehe).
markzzz
Using firebug I made the body height 900px high, the scroll-up was happening with small gaps, like as if it was slowly climbing up, It's like you were distorting the page layout, and then restoring it, which depending on your browser, might have different effects, in firefox it just resulted in a small jump up.
YuriKolovsky