You are here: Keeping scroll position « PHP Programming Language « IT news, forums, messages
Keeping scroll position

Posted by Lancelot on 12/01/06 11:20

Hi,

I want to keep the scroll position after reload. I tried this:

function f_scrollTop() {
// returns scroll position
}

function scrollPosition() {
document.buy_from_shop_form.scrollpos.value = f_scrollTop();
}

<form name="buy_from_shop_form" id="buy_from_shop_form" method="post"
action="">
<input type="hidden" name="cd_price" id="cd_price" value="<?php echo
$rel[8]; ?>" />
<input type="hidden" name="cd_id" id="cd_id" value="<?php echo
$rel[0]; ?>" />
<input type="hidden" name="scrollpos" id="scrollpos" value="" />
<input type="submit" name="buy_from_shop_submit"
id="buy_from_shop_submit" value="Buy CD" onClick="scrollPosition()" />
</form>

It didn't work. When I did a print_r($_POST), the scrollpos field did not
contain anything. So I tried saving the scroll position in a cookie instead:

function scrollPosition() {
document.cookie = 'sp=' + f_scrollTop();
}

print_r($_COOKIE) revealed that the scroll position was stored in the
cookie. I removed the scrollpos hidden field from the form and added this
code to the script:

if(isset($_COOKIE['sp'])) {
echo "<script type = 'text/javascript'>\n";
echo "window.pageYOffset=".$_COOKIE['sp'];
echo "</script>";
}

When that did not work, I tried

echo "document.documentElement.scrollTop = " . $_COOKIE['sp'];


instead, and when that didn't work either, I tried

echo "document.body.scrollTop = " . $_COOKIE['sp'];

which also did not work.

I suspect the reason I cannot get this to work is that I haven't properly
understood how Php and Javascript are parsed/processed/rendered. I have
searched the Web for articles about it, but none of the articles I found
were detailed enough to give me a thorough understanding of the mechanisms.

Help would be greatly appreciated.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация