Skip to content Skip to sidebar Skip to footer

Why Is My Javascript Not Working Correctly In Strict Mode On Safari?

I have built a website that uses some simple JavaScript. After some testing, I have found that my JavaScript is behaving very differently on iOS devices as compared to all other de

Solution 1:

The possible problem

Your problem may be that you have made a const declaration with strict mode enabled.


A possible solution

If that is the case, and you would like to continue using strict mode, one solution is to use var instead of const, and simply be more careful to treat the variable as a constant.


More on this problem

It is in fact difficult to debug this problem without an OS X computer, because you are unable to use iOS Safari's remote console "Web Inspector".

However, according to caniuse, for the current versions of both Safari and iOS Safari (9.1 and 9.3, respectively, as of writing) const is:

Only recognized when NOT in strict mode

You can test this for yourself using the following example (or this JSFiddle). This code worked as expected for me with Chrome and Firefox on both Ubuntu and Android, but not on Firefox for iOS or iOS Safari.

HTML


<pre></pre><form><inputtype="submit"></form>

JS


"use strict";

var form = document.querySelector("form");
var info = document.querySelector("pre");

// This const declaration in conjunction with strict mode will// cause this script not to work on iOS devicesconst _MEANINGLESS = 0;

form.addEventListener("submit", function(event) {
    event.preventDefault();
    info.textContent = "Submit event captured.\n";
});

Post a Comment for "Why Is My Javascript Not Working Correctly In Strict Mode On Safari?"