PDA

View Full Version : Passing a function from page to page


Dude101
05-13-2003, 08:34 PM
I put an applet on a website that scrolls an image across the screen. When someone clicks on part of the image he is sent to a new window in that frame describing the area pictured. Only problem I'm having is when he returns to the original scrolling image he has to start at the beginning of the scroll.
I'd like to return him to the point in the image where he left.

I wrote the author of the applet and he wrote the following that exceeds my knowledge of JS. Any help supplying an example would be appreciated.

"I have take a look at your page and it looks great! You can make the scroller start at the same point when the user returns to the page by JavaScript. It's require some knowledge of JavaScript, and I only have time to point out a solution: Use the JavaScript onLoad() and onRefresh() functions to call your own function which will call the scrollers jump(x,y) function. In your JavaScript code you have to store the users position when leaving the page by the getCursorX() and getCursorY(). The JavaScript variables have to be stored in variable declared as var, eg var x,y; and prefeable in a JavaScript sourcefile, which speed it all up."

The page is located at: www.gokadar.com/base_pages/tour/example3/tour3.htm

I tried this:
<a href="../panorama.html JavaScript:jump(1714,40)" target="scroller" >Return
to Panorama</a>

after I placed the jump function that came with the applet in the heading section:
// Functions to jump to last location
function jump(x,y)
{
document.scroller.setPos(x,y);
}

Am I even close on this one? Don't laugh too hard..

dude101

InZa
05-17-2003, 12:45 AM
sigh, I just wrote a long text explaining what to do and submitted my reply but it then said I wasn't logged in. Lost all the text I had just typed. Oh well, here we go again:

First of all never use the same name twice for elements on your page(!!!) You've named one of your frames "scroller" plus you've named your panorama applet "scroller". This is no good. In my example I renamed the frame to "panorama". Please note that by doing this you also have to edit your panorama.txt document to replace all occurances of "#scroller" with "#panorama" so that your links will open in the right frame.

I am no big fan of frames, but in this case I am happy your site is using frames, since I can then use a frame to store the x and y variables of the panorama. You could choose to store the information in a cookie instead allowing for the user to refresh the whole page, and the panorama would still start in the position he left it. However I see no reason for that, and since you're already using frames I think this is the way to go. Well then. In your frameset page add a frame:
<frame name="settings" marginwidth="0" marginheight="0" scrolling="NO" src="settings.html">
You can copy the source for this frame from my example: http://www.innocently-evil.net/inza/panorama/settings.html

Now you need to add the code to your "panorama.html" page. In the <body> tag add these attributes:
onload="jump()" onunload="saveCoords()"
When the page is finished loading it'll then call the jump function which makes the panorama jump to the saved x/y position. The saveCoords function is executed just before the user leave the page and it takes the current x/y coordinates from the applet and stores them in variables in the settings frame.

I changed the code of jump function so you need to replace it with this:
function jump(x,y)
{
document.scroller.setPos(parent.frames.settings.panoramaXPos, parent.frames.settings.panoramaYPos);
}

plus add the function that saves the variables:

function saveCoords()
{
parent.frames.settings.panoramaXPos = document.scroller.getX();
parent.frames.settings.panoramaYPos = document.scroller.getY();
}

You can also just copy/paste the code from my example.

Ok, this should be all. I hope I didn't forget anything. In case you can't get it to work let me know and put up an example so I can find the error.

This is the example (note that the only link that works in my example is the "Clinic Area" link):
http://www.innocently-evil.net/inza/panorama/
Hmm.. I hope the link works for you, 'cause it doesn't for me when I click, but if I copy/paste in a new browser window the applet loads. It's prolly because of cached content after working with it, but I dunno...

I just had someone check the link for me. At first the applet didn't load and there was a js error because of that. Then (after refreshing) the applet showed up, but the error remained to be there. Third time was the charm and both applet plus js worked. So it seems there's a problem when the applet hasn't started but the page has loaded - the jump function tries to access a function of the applet, but can't because it's not finished loading the panorama image. I guess it'd work if you put an error handler like:

try {
document.scroller.setPos(parent.frames.settings.panoramaXPos, parent.frames.settings.panoramaYPos);
}
catch (e) {
}

But I'm not sure.. Does the page work for you the first time you load it?