6

I have a script on my site that works in every browser, except Internet Explorer. Can someone explain why this doesn't work?

var header = document.getElementById('header');
var picturebg = document.getElementsByClassName('picturebg')[0];
var arrow = document.getElementsByClassName('toTop-transparent')[0];
var supportPageOffset = window.pageXOffset !== undefined;

window.onscroll = function() {
  "use strict";
  var y = window.scrollY;

  if (y > 7) {
    header.className = 'header-colored';
    arrow.className = 'toTop';
    picturebg.className = 'picturebgns';
  } else {
    header.className = 'header-transparent';
    arrow.className = 'toTop-transparent';
    picturebg.className = 'picturebg';
  }
};

The console doesn't give any errors, it just doesn't work. I have another jQuery script that runs just fine.

I've seen the other question here about the same thing, but it didn't help in any way.

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
r4tchet
  • 157
  • 1
  • 2
  • 8
  • Have you used www.caniuse.com to check the compatibility of functions you're using? – Carcigenicate Nov 30 '16 at 20:28
  • Well, that snippet doesn't work because you don't have any HTML in it, so document.getElementById() returns null.... – Heretic Monkey Nov 30 '16 at 20:28
  • If you already have jQuery loaded, why not just port this to jQuery? One of the benefits of jQuery is that its methods is tested to work on all modern browsers. – junkfoodjunkie Nov 30 '16 at 20:30
  • @junkfoodjunkie this is for a school assignment where jQuery is frowned upon, so I'm using as much pure js as I can :) – r4tchet Nov 30 '16 at 20:33
  • I see. I can understand that, but as long as the jQuery library is loaded, albeit for other reasons, it just makes sense using it - the overall amount of JS will be mostly the same, actually probably slightly less, since selectors and names in jQuery is a bit more straight-forward. – junkfoodjunkie Nov 30 '16 at 20:37
  • Two big problems. 1) You didn't specify WHICH version of Internet Explorer you tested with. 2) You didn't bother mentioning what it was supposed to do, which I think is hide the header or something. But I think it may be an issue with scrollY in IE. http://stackoverflow.com/questions/16618785/ie8-alternative-to-window-scrolly – Jason Livesay Dec 01 '16 at 00:47

1 Answers1

9

Certain properties used in your snippet are not supported by IE.

From the MDN page on scrollY:

For cross-browser compatibility, use window.pageYOffset instead of window.scrollY. Additionally, older versions of Internet Explorer (< 9) do not support either property and must be worked around by checking other non-standard properties.1

It appears you already have found the check for pageOffset support, so also check if non-standard properties are supported (e.g. CSS1 is compatible):

var isCSS1Compat = ((document.compatMode || "") === "CSS1Compat");

var y = supportPageOffset ? window.pageYOffset : isCSS1Compat ? document.documentElement.scrollTop : document.body.scrollTop;

Try this sample, using addEventListener() instead of onscroll.

var header = document.getElementById('header');
var picturebg = document.getElementsByClassName('picturebg')[0];
var arrow = document.getElementsByClassName('toTop-transparent')[0];
var supportPageOffset = window.pageXOffset !== undefined;
var isCSS1Compat = ((document.compatMode || "") === "CSS1Compat");



header.addEventListener('scroll', function(event) {
  "use strict";
  var y = supportPageOffset ? window.pageYOffset : isCSS1Compat ? document.documentElement.scrollTop : document.body.scrollTop;
console.log('y: ',y);
  if (y > 7) {
    header.className = 'header-colored';
    arrow.className = 'toTop';
    picturebg.className = 'picturebgns';
  } else {
    header.className = 'header-transparent';
    arrow.className = 'toTop-transparent';
    picturebg.className = 'picturebg';
  }
});
<div id="header" style="height: 50px; overflow: scroll;">
  <img  class="picturebg" src="https://www.gravatar.com/avatar/684fa9ff80577cbde88ffbdebafac5b4?s=32&d=identicon&r=PG&f=1" />
  <div id="arrow" class="toTop-transparent">&darr;</div>
  </div>

1https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollY#Notes

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58