views:

799

answers:

3

I am writing a game in PHP/Zend Framework and Facebook JavaScript. The gameboard is displayed in a scrolling window. To view the entire gameboard, the user must scroll around. Each step of the game causes a page refresh, so when the user moves on to another step, their scroll postion is lost and the window is returned to it's default position. I'm wondering if there is a way to automatically have that scrolling window return to the position it was in before the page was refreshed so that the user does not have to scroll back to where they were? Thanks!

+3  A: 

If you're creating a link-based game, I'd use AJAX to send the data of your click to your server, process it, and return it to the user without refreshing the page at all.

This will prevent you from having to scroll around the page using JS, and it'll also make playing the game much faster and more enjoyable for the users.

BraedenP
+1. Page auto-scroll is incredibly annoying
too much php
I hear ya, and I totally agree. The problem is that I'm really new to AJAX. I've got some AJAX code in my app, but to write the entire app in AJAX seems like it would result in this huge frontend app with frequent backend calls. I'm familiar with organizing my app in Zend Framework, but I don't have a clue on how to organize a large frontend app like that, hence I'm sticking with what I know.
Chris Barnhill
+2  A: 

Save the position in the onunload (or after each scrolling event) in the Window.name or in a cookie and then scroll to that point when page loads.
But, the Ajax solution is a better approach to what you do.

Itay Moav
I tried this solution, but I couldn't get it to work because I am using Facebook JS and I don't have access to the <body> tag, so I can't implement onload/onunload. I found a work-around for onload, but not for onunload. I tried using setInterval() to update a cookie with the current scroll position, but then I also discovered that FB disables access to cookies ): Ajax would involve re-writing most of my application, and I simply don't have the time. I did consider using setInterval() and an ajax call to send the scroll coords to the backend every few seconds or so. Does this seem ...
Chris Barnhill
like it would overwhelm the server to get these ajax calls from every user every 1-5 seconds?
Chris Barnhill
Really depends on the server...You can add events to the body dynamically inside script tags. something on the lines of: body.onload=f; //f is a function. Of course you need to figure out the right syntax.
Itay Moav
A: 

Use <a name="game"> and refresh to gamePage.html#game

Lukman
But that would involve adding an anchor to every click-able element on what I'm assuming is a 'tiled' game board. That'd be a heck of a lot of anchors.
BraedenP
PHP could add a single anchor at the desired location.
too much php
No....the anchor should be created after each scrolling event. Sounds like a lot oh headache, and I don't think you can change the url of the page without causing the page to refresh.
Itay Moav
Well you can re-load the page's contents without a refresh, and that's where the AJAX comes in. Seriously Chris, if you want your users to be happy, AJAX is the ONLY way to go. ;)
BraedenP