Gosundi
Gosundi

Reputation: 45

Remember Page Position Using Two Lines of Script

In all major browsers, except Internet Explorer, the following script returns the page to its previous vertical position on reload:

<?php $y = $_COOKIE["y"]; ?> //in head tag before any output

and

<?php
print "<body onScroll=\"document.cookie='y=' + window.pageYOffset\" onLoad='window.scrollTo(0,$y)'>";

Can someone please tell me how I would modify this code to remember the page's vertical position in IE? Thanks.

Upvotes: 2

Views: 595

Answers (2)

Sammitch
Sammitch

Reputation: 32242

I use the following code to do basic IE/not-IE browser detection:

if(document.all) {  //if IE
    //code
} else {            //if not IE
    //code
}

You should be able to combine this with AlecTMH's document.body.scrollLeft and document.body.scrollTop suggestion to get where you're going. But you're likely going to have to write a function for it and then call that in onScroll().

I'm not exactly a JavaScript wiz, but...

function blah() {
    if(document.all) {  //if IE
        document.cookie='y=' + document.body.scrollTop
    } else {            //if not IE
        document.cookie='y=' + window.pageYOffset
    }
}

...might almost be functional code.

Upvotes: 1

AlecTMH
AlecTMH

Reputation: 2725

From w3Schools :

IE 8 and earlier does not support this property, but may use "document.body.scrollLeft" and "document.body.scrollTop" instead.

Upvotes: 2

Related Questions