So here’s one tht was driving me spare this morning. A WordPress theme I’ve been working on (which I’ll talk about when it goes live later this month) uses a few Javascript libraries to do some funky things with PDF’s. I’ve been building it for a few months now and we moved it into final testing this week and, more fool me, I realised I’d not looked at it in IE which is still a commonly used browser at the University.

I shouldn’t have been surprised, really, when my code fell over in IE but I was surprised with, ultimately, WHY it fell over. One thing that struck me was the errors that appeared in the console, namely identifying a missing right parenthesis that wasn’t, to my mind, actually an error. Here’s the line:

function scrollToAnchor(aid,offset=0)

So, what exactly was the problem here. Well, quite simply, it’s that offset=0 bit, assigning the default value to the variable being passed. Unbeknownst to me, this fairly common coding practice is only supported in Javascript ES6. What version does IE11 use? ES5. So what doesn’t it support? Assigning default values to variables…

So, to get past this I’ve changed the function to read:

function scrollToAnchor(aid,offset){
    if(!offset) offset = 0;
...}

Which checks the offset value and sets it to 0 if it hasn’t been passed. Seems hugely counterproductive but it’s a fairly simple fix to an issue that was erroring and throwing out all my JS calls.

Hope this is helpful to someone! Shout in the comments below if you’ve got any thoughts.

Advertisements