View Full Version : reloading & repositioning frames
mighty_muke
12-17-2001, 09:52 PM
Hi,
I have a problem where I need to reload a frame, then reposition it in exactly the same place as it was, from another frame. Basically I want to mimic a user pressing the browser refresh button. The code I am using is...
var pageoffset;
var currentframe;
currentframe = parent.frame_name;
// Grab frames current scroll position
pageoffset = currentframe.document.body.scrollTop;
// Reload frame
currentframe.location.href = currentframe.location;
// Reset frames scroll position
currentframe.document.body.scrollTop = pageoffset;
The problem is that there seems to be a delay for the page to reload, and I am setting the scrollTop before it has finished reloading - hence it is always going back to the top of the page. I would prefer not to place any code into the frame to be reloaded, and the property currentframe.document.readyState seems to be of no help at all.
Any help would be appreciated.
MM.
Jon Hanlon
12-18-2001, 04:32 PM
I don't understand.
When you load a new page into a frame then it does load it in exactly the same position - automatically.
mighty_muke
12-18-2001, 06:19 PM
OK - as I'm not to hot at explaining myself, I have created a simple example at the following location
http://user1.7host.com/mightymuke/carina/
The code for the three pages on this site are inluded in the attachment.
As you will be able to see from the example, if you scroll down, then press the login button, you do not stay scrolled down.
Any ideas on improving the code would be appreciated.
Thanks.
mighty_muke
12-18-2001, 06:22 PM
Try for the attachment again...
scoutt
12-18-2001, 06:22 PM
looks like all you are doing is loading 2 pages with one button click. so when you login you click the button and reload left and right frames.
Jon Hanlon
12-18-2001, 08:48 PM
Oh, I see.
Well, HTTP is a stateless protocol, which means it forgets everything in an instant. If you want data to persist then you have to do it yourself.
Firstly, a primer on frames. Frames communicate through their parent. In their common parent is a frames collection containing all of the frames objects.
For instance, if we have two frames called bart and lisa, then bart refers to the saxaphone (say) property of lisa by:
parent.frames.lisa.saxaphone
frames are instances of the window class, that is, they are window objects.
So....
When the user submits the form, you need to include in the form some information about the other frames scroll. Then, in the page generated by ASP you need to include an onload function that will scroll that page by the required amount.
Or....
When the user submits a form, set a timeout that checks every half second or so to see if the other frame has reloaded, and then set the scroll.
function onFormSubmit() {
var scrollAmount = parent.frames.other_frame_name.document.body.scrollTop;
var str = "waitAround" + "(" + scrollAmount + ")"
gScrollTimeOut = window.setTimeout(str,500)
}
function waitAround(scrollAmount) {
var targetObj = parent.frames.other_frame_name;
if (!targetObj || targetObj.document.readyState != "complete") { // don't exist or not ready
var str = "waitAround" + "(" + scrollAmount + ")"
gScrollTimeOut = window.setTimeout(str,500) // 500 millisecs
return false; // reset timeOut & PO
}
targetObj.scrollTo(0,scrollAmount);
}
I think this was the method you were using.
You should however, submit a (hidden) form on the other frame instead of the side frame if you are using this method. Otherwise the side frame is destroyed when it submits.
In other words, on the rhs frame have a hidden form:
<form name=rhsHidden action="whatever.asp">
<input type=hidden name=username value="">
<input type=hidden name=password value="">
And on the lhs (side) frame, have:
<form onsubmit="return submitRHS(this.form)">
function submitRHS(formObj) {
var otherFrame = parent.frames.other_frame_name;
otherFrame.forms.rhsHidden.username.value = formObj.username.value;
otherFrame.forms.rhsHidden.password.value = formObj.password.value;
otherFrame.forms.rhsHidden.submit();
onFormSubmit();
return false; // needed to stop this form submitting
}
Or you could use target="..."
Oh, and get rid of that popup window. It's so annoying.
Hope this helps.
mighty_muke
12-18-2001, 10:43 PM
Fantastic - that worked great.
Thanks a lot!
BTW - apologies for the pop-up window. All I can say is that it is a free host, and the final product will not be there.
Thanks again. I cant believe I wasted so much time on something that should have been so simple.
Just out of curiosity, I had implemented a loop to check the readystate of the second frame, but it was always stuck on "complete". Does the browser not multitask, and reload one frame while another is busy doing something? I assume this was the purpose of the window timeout method?
Thanks,
MM.
Jon Hanlon
12-19-2001, 05:01 PM
That's correct.
The only way you can create a new 'thread' is to use setTimeout() or setInterval().
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.