The Little Guy Posted August 29, 2008 Share Posted August 29, 2008 Just an out of the blue question. I noticed that on my sites, that when I refresh the page, the page starts at the very top, and isn't automatically scrolled down to where I was when I refreshed the page. It only happens when I add php to the site, such as some mysql stuff, and dynamic info. Unlike on this board, if you refresh the page, it remembers how far you were scrolled down. Why do my sites do that? Quote Link to comment https://forums.phpfreaks.com/topic/121917-page-refresh/ Share on other sites More sharing options...
Zane Posted August 29, 2008 Share Posted August 29, 2008 if you look in the URL http://www.phpfreaks.com/forums/index.php/topic,214242.html#msg978053 this part right here ....#msg978053 tells the page how far to go down What is it? Well, if you know CSS you don't need that answer, but that's the id of one particular area on the page. You'll have to use id's to do what you're looking for. Quote Link to comment https://forums.phpfreaks.com/topic/121917-page-refresh/#findComment-629148 Share on other sites More sharing options...
kratsg Posted August 29, 2008 Share Posted August 29, 2008 This is more of a javascript question, but I've got a piece that I've posted somewhere before as a solution that's helped me on some of my pages that needed it (forms, etc) function getScrollXY() { var scrOfX = 0, scrOfY = 0; if( typeof( window.pageYOffset ) == 'number' ) { //Netscape compliant scrOfY = window.pageYOffset; scrOfX = window.pageXOffset; } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) { //DOM compliant scrOfY = document.body.scrollTop; scrOfX = document.body.scrollLeft; } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) { //IE6 standards compliant mode scrOfY = document.documentElement.scrollTop; scrOfX = document.documentElement.scrollLeft; } return [ scrOfX, scrOfY ]; } This function, when called, will return the current "scroll" position. Then, set cookies in javascript when the user leaves the page (or something) so when they refresh, they go back to where they were.... You can check the page the browser is leaving to navigate to, see if it matches with the current page, and use that as a conditional for setting/reading cookies. Quote Link to comment https://forums.phpfreaks.com/topic/121917-page-refresh/#findComment-629150 Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 if you look in the URL http://www.phpfreaks.com/forums/index.php/topic,214242.html#msg978053 this part right here ....#msg978053 tells the page how far to go down What is it? Well, if you know CSS you don't need that answer, but that id of one particular area. You'll have to use id's to do what you're looking for. Yeah, except for the fact that there's no hash on the URL when clicking the topic, scrolling to a point, then refreshing. If you provide someone with a link to a certain post, then yeah, you're right, but that's not the case here. Quote Link to comment https://forums.phpfreaks.com/topic/121917-page-refresh/#findComment-629154 Share on other sites More sharing options...
Fadion Posted August 29, 2008 Share Posted August 29, 2008 What is it? Well, if you know CSS you don't need that answer, but that's the id of one particular area on the page. You'll have to use id's to do what you're looking for. Actually CSS comes to no help in this. Instead of the ID attribute, one can use the "name" attribute, that shares the same name space as the former. Even though, those are html attributes. The CSS selectors (id, class, etc) are a different matter. Quote Link to comment https://forums.phpfreaks.com/topic/121917-page-refresh/#findComment-629167 Share on other sites More sharing options...
kratsg Posted August 29, 2008 Share Posted August 29, 2008 if you look in the URL http://www.phpfreaks.com/forums/index.php/topic,214242.html#msg978053 this part right here ....#msg978053 tells the page how far to go down What is it? Well, if you know CSS you don't need that answer, but that id of one particular area. You'll have to use id's to do what you're looking for. Yeah, except for the fact that there's no hash on the URL when clicking the topic, scrolling to a point, then refreshing. If you provide someone with a link to a certain post, then yeah, you're right, but that's not the case here. Yes, that would work... But if you just go to this topic, scroll down, then refresh, it does bring you back to that same point, regardless of whether or not you clicked on an anchor tag o_o Quote Link to comment https://forums.phpfreaks.com/topic/121917-page-refresh/#findComment-629171 Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 if you look in the URL http://www.phpfreaks.com/forums/index.php/topic,214242.html#msg978053 this part right here ....#msg978053 tells the page how far to go down What is it? Well, if you know CSS you don't need that answer, but that id of one particular area. You'll have to use id's to do what you're looking for. Yeah, except for the fact that there's no hash on the URL when clicking the topic, scrolling to a point, then refreshing. If you provide someone with a link to a certain post, then yeah, you're right, but that's not the case here. Yes, that would work... But if you just go to this topic, scroll down, then refresh, it does bring you back to that same point, regardless of whether or not you clicked on an anchor tag o_o Yeah, I know, that's what I said. Quote Link to comment https://forums.phpfreaks.com/topic/121917-page-refresh/#findComment-629176 Share on other sites More sharing options...
The Little Guy Posted August 29, 2008 Author Share Posted August 29, 2008 I believe that this has nothing to do with CSS, or JavaScript. Here are my examples (go to both sites) Example 1 (go here): http://www.google.com/search?hl=en&q=free&btnG=Google+Search&aq=f&oq= Scroll down the page, then refresh it. Example 2 (go here): http://phpsnips.com/snippet.php?id=16 Scroll down the page, then refresh it. As you can see, in example one the browser remembered how far down the page you were when you refreshed the page, where as in example 2, it started you back at the top of the page after you refreshed the page. Why does this happen on all my sites? Quote Link to comment https://forums.phpfreaks.com/topic/121917-page-refresh/#findComment-629203 Share on other sites More sharing options...
Zane Posted August 29, 2008 Share Posted August 29, 2008 This may help you http://patrickfoley.com/2005/01/21/scroll-saver/ Quote Link to comment https://forums.phpfreaks.com/topic/121917-page-refresh/#findComment-629237 Share on other sites More sharing options...
kratsg Posted August 29, 2008 Share Posted August 29, 2008 I believe that this has nothing to do with CSS, or JavaScript. Here are my examples (go to both sites) Example 1 (go here): http://www.google.com/search?hl=en&q=free&btnG=Google+Search&aq=f&oq= Scroll down the page, then refresh it. Example 2 (go here): http://phpsnips.com/snippet.php?id=16 Scroll down the page, then refresh it. As you can see, in example one the browser remembered how far down the page you were when you refreshed the page, where as in example 2, it started you back at the top of the page after you refreshed the page. Why does this happen on all my sites? Javascript is almost literally the only way to do it buddy... All those fancy web applications made use SOME javascript. Google Suggest uses javascript to communicate with its servers to bring results to your browser as you type... Flash uses actionscript (which is quite similar to javascript). Javascript is the kind of code that works ONLY client side (but it can communicate with other pages though). This is why you need javascript, because scrolling/scroll position is a client-side action. Quote Link to comment https://forums.phpfreaks.com/topic/121917-page-refresh/#findComment-629263 Share on other sites More sharing options...
The Little Guy Posted August 29, 2008 Author Share Posted August 29, 2008 Why when you take a static HTML page, and refresh (F5), the browser know exactly how far down it was scrolled, and when you make a dynamic php page, and refresh (F5) the browser no longer know how far down the page it was scrolled, so it starts at the top? Quote Link to comment https://forums.phpfreaks.com/topic/121917-page-refresh/#findComment-629282 Share on other sites More sharing options...
kratsg Posted August 29, 2008 Share Posted August 29, 2008 These forums are a dynamic PHP page, yet it remembers where you were because of javascript. Not all .html pages are static. They could still parse PHP (if set in .htaccess to do so) and quite simply, this is never true. Javascript is the only tested-tried-true method of actually doing this. Quote Link to comment https://forums.phpfreaks.com/topic/121917-page-refresh/#findComment-629290 Share on other sites More sharing options...
The Little Guy Posted August 30, 2008 Author Share Posted August 30, 2008 Could you show me the JavaScript code that does this? Quote Link to comment https://forums.phpfreaks.com/topic/121917-page-refresh/#findComment-629332 Share on other sites More sharing options...
kratsg Posted August 30, 2008 Share Posted August 30, 2008 Could you show me the JavaScript code that does this? function getScrollXY() { var scrOfX = 0, scrOfY = 0; if( typeof( window.pageYOffset ) == 'number' ) { //Netscape compliant scrOfY = window.pageYOffset; scrOfX = window.pageXOffset; } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) { //DOM compliant scrOfY = document.body.scrollTop; scrOfX = document.body.scrollLeft; } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) { //IE6 standards compliant mode scrOfY = document.documentElement.scrollTop; scrOfX = document.documentElement.scrollLeft; } return [ scrOfX, scrOfY ]; } This is part of what I found in the forum's files. It's integrated into the forum. But I'm sure that on page load, it will recall the scroll positions and automatically scroll you down to that point. **Just checked the processes run on page load, there are some functions being called to this extent, so my suspicion is confirmed. Quote Link to comment https://forums.phpfreaks.com/topic/121917-page-refresh/#findComment-629339 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.