Why is my JavaScript not working correctly in strict mode on Safari?

前端 未结 1 1793
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-26 08:37

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

相关标签:
1条回答
  • 2021-01-26 09:27

    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>
        <input type="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 devices
    const _MEANINGLESS = 0;
    
    form.addEventListener("submit", function(event) {
        event.preventDefault();
        info.textContent = "Submit event captured.\n";
    });
    
    0 讨论(0)
提交回复
热议问题