473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Keeping scroll position

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.
Dec 1 '06 #1
1 4205
Lancelot wrote:
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.
Hi,

Understanding JS and PHP interact is very simple if you follow these 3
rules:
1) Always check the HTML-source produced by PHP
2) Always check the HTML-source produced by PHP
3) Always check the HTML-source produced by PHP

From a browsers point-of-view there is little/no difference if PHP produced
the HTML, or a blue goblin did.
So try to make a plain HTML-version first that gets the job done.
THEN think about PHP automating that job.

Regards,
Erwin Moller
Dec 1 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

179
by: SoloCDM | last post by:
How do I keep my entire web page at a fixed width? ********************************************************************* Signed, SoloCDM
4
by: ojorus | last post by:
Hi! I just wonder how I can save a page's scroll position with javascript. (i'm not a javascript developer) I have a PHP-page with two columns; the left contains a lot of thumbnails, and the right...
4
by: Martin Hesselbach | last post by:
Hi folks, I was trying to "pin" a menubar to the top of a site. It should allways stay on its absolute position. Especially when the user scrolls the page... I did like this: <div...
2
by: Alberto | last post by:
¿Can I retain the scroll position in a long page after a post back? Thank you
1
by: JC | last post by:
I'm sure you've all seen the save scroll position from 4 guys from rolla which can be found here > http://aspnet.4guysfromrolla.com/articles/111704-1.aspx BUT try to get that to work AND still be...
1
by: marty.overdear | last post by:
>NET 2003, vb code behind. I am having a problem with the look of a page with a datagrid. I have the grid populated, and it can be a couple of hundred rows long. If the user is down toward the...
0
by: Rune Jacobsen | last post by:
Hi all, In my project I have a control (described in an earlier post) which basically consists of three "subcontrols": One big area where the vital data is displayed (scrollable, since it will...
7
by: Lit | last post by:
Hi, How can I capture the vertical scroll bar position for a Listbox. I have a Listbox of 100 items + when I click on it I post back remove the item selected. After returning to the client...
12
Frinavale
by: Frinavale | last post by:
I think I'm trying to do something impossible. I have a <div> element with a overflow style set to "scroll". In other words my <div> element allows the user to scroll the content within it. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.