return scrollbar to position-not working in opera/ff

R

Ross

I have been using the following script to return a scrollbar to the position
it was in before the data was posted. It works in ie but not in firefox.

Thanks,

R.

<script type="text/javascript">
window.onload = function(){
var strCook = document.cookie;
if(strCook.indexOf("!~")!=0){
var intS = strCook.indexOf("!~");
var intE = strCook.indexOf("~!");
var strPos = strCook.substring(intS+2,intE);
document.getElementById("menu_holder_inside").scrollTop = strPos;
}
}
function SetDivPosition(){
var intY = document.getElementById("menu_holder_inside").scrollTop;
// document.title = intY;
document.cookie = "yPos=!~" + intY + "~!";
}


</script>

and on the div

<div id="menu_holder_inside" onscroll="SetDivPosition()">
 
G

Gérard Talbot

Ross a écrit :
I have been using the following script to return a scrollbar to the position
it was in before the data was posted. It works in ie but not in firefox.

Thanks,

R.

<script type="text/javascript">
window.onload = function(){

Your function should execute on the load event on the body, not on the
window. The window may be created, but the DOM tree (with all its
elements, images, structure, headings, etc.) might not.

var strCook = document.cookie;
if(strCook.indexOf("!~")!=0){
var intS = strCook.indexOf("!~");
var intE = strCook.indexOf("~!");
var strPos = strCook.substring(intS+2,intE);
document.getElementById("menu_holder_inside").scrollTop = strPos;

and here, you're querying for an object which may not have been yet
parsed, rendered in the document content.
}
}
function SetDivPosition(){
var intY = document.getElementById("menu_holder_inside").scrollTop;
// document.title = intY;
document.cookie = "yPos=!~" + intY + "~!";
}


</script>

and on the div

<div id="menu_holder_inside" onscroll="SetDivPosition()">

I see nothing wrong with your code. Personally, I would ditch that
function, cookie handling, string parsing, value storing, etc.., and
then just resort to scrollIntoView() instead. Something like this:

<body
onload="document.getElementById('menu_holder_inside').scrollIntoView(true);"
....>

scrollIntoView is supported by MSIE 5+ and Mozilla 1.5+, Firefox 1.x, NS
7.x, etc.

Gérard
 
G

Gérard Talbot

Gérard Talbot a écrit :
Ross a écrit :



Your function should execute on the load event on the body, not on the
window. The window may be created, but the DOM tree (with all its
elements, images, structure, headings, etc.) might not.




and here, you're querying for an object which may not have been yet
parsed, rendered in the document content.



I see nothing wrong with your code.

[snipped]
Forget that part on scrollIntoView().

I think your code is correct except it should be the load event on the
body node, not on the window (asychronous events). Window load event
always fires before the body load event since their content and tasks
involved are different.

Gérard
 
R

RobG

Ross said:
I have been using the following script to return a scrollbar to the position
it was in before the data was posted. It works in ie but not in firefox.

This link will explain your problem with 'onscroll':

<URL:http://www.quirksmode.org/js/events_compinfo.html>

Onscroll is a method of the window object, it is not a W3C standard and
belongs to DOM Level 0 (i.e. inherited from pre-W3C browsers).

<URL:http://www.mozilla.org/docs/dom/domref/dom_window_ref72.html#1018974>



This link deals with the issue with scrollTop:

<URL:http://www.quirksmode.org/viewport/compatibility.html>

All other browsers use pageYOffset, IE 6 uses
document.documentElement.scrollTop (in strict mode), for all other IEs
and 6 in quirksmode use document.body.scrollTop.

You need to feature test (a cross-browser function is provided at the
link to determine how much the page has scrolled - search for scrollTop
or pageYOffset).

[...]
 
G

Gérard Talbot

RobG a écrit :
You snipped important chuncks of code from the OP. Here it is:


{
<script type="text/javascript">
window.onload = function(){
var strCook = document.cookie;
if(strCook.indexOf("!~")!=0){
var intS = strCook.indexOf("!~");
var intE = strCook.indexOf("~!");
var strPos = strCook.substring(intS+2,intE);
document.getElementById("menu_holder_inside").scrollTop = strPos;
}
}
function SetDivPosition(){
var intY = document.getElementById("menu_holder_inside").scrollTop;
// document.title = intY;
document.cookie = "yPos=!~" + intY + "~!";
}


</script>

and on the div

<div id="menu_holder_inside" onscroll="SetDivPosition()">


}

His function name is SetDivPosition. His function name is not
SetPagePosition nor SetWindowPosition or something like that.

This link will explain your problem with 'onscroll':

<URL:http://www.quirksmode.org/js/events_compinfo.html>

Onscroll is a method of the window object, it is not a W3C standard

DOM 3 Events interface compliant, scroll event for element:
"scroll
A document view or an element has been scrolled. The scroll occured
before the dispatch of this event type."
http://www.w3.org/TR/2003/NOTE-DOM-Level-3-Events-20031107/events.html#event-scroll

and
belongs to DOM Level 0 (i.e. inherited from pre-W3C browsers).

MSIE 6, Mozilla 1.x, Firefox 1.x, NS 7.x and a bunch of other browsers

How about testing this file then?:

http://www.mozilla.org/docs/dom/domref/scrollTop.html#Example

This link deals with the issue with scrollTop:

<URL:http://www.quirksmode.org/viewport/compatibility.html>

All other browsers use pageYOffset,


pageYOffset is a property of the window object (NS 4+, Mozilla 1.x,
Firefox 1.x, Opera 6+, etc.)
pageYOffset is not a property of the scrollable elements like a <div>.


IE 6 uses
document.documentElement.scrollTop (in strict mode), for all other IEs
and 6 in quirksmode use document.body.scrollTop.

That's for a document scroll-view. What about a div? That is what the OP
asked..., no?
You need to feature test (a cross-browser function is provided at the
link to determine how much the page has scrolled - search for scrollTop
or pageYOffset).

[...]

http://developer.mozilla.org/en/docs/DOM:element.scrollTop#scrollTop

http://www.gtalbot.org/BugzillaSection/DocumentAllDHTMLproperties.html

The OP is right regarding Opera 7+. Opera does not support registering a
scroll event listener to a scrollable element. I.e.:
ElementReference.addEventListener("scroll", functionName, false);
is not supported for Opera 7+. But DOM 3 Events compliant browsers
support the scroll event for elements.

http://www.w3.org/TR/2003/NOTE-DOM-Level-3-Events-20031107/events.html#event-scroll

And pretty much all modern browsers support MSIE's DHTML object model.

Gérard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top