iOS safari crashing (a problem repeatedly occured)

前端 未结 5 2004
春和景丽
春和景丽 2021-02-14 21:47

I\'m developing a website and have recently run into a problem when testing on my iPhone X - the site wont load.

Safari tries to load it, then reports the error \'this

5条回答
  •  天命终不由人
    2021-02-14 21:56

    I ran into this issue today and wanted to see the MRE that would cause this to happen. It does seem to be the case that both Safari and Chrome on iOS 14 crash when the autofocus attribute is set on at least two controls, and focus is then requested on either control using JavaScript. I was able to confirm that the crash doesn't occur on iOS <= 13. Chrome 87 and Safari 13.1 on macOS are also unaffected.

    Whether the crash occurs depends on when focus is requested. In the 'window load' event, things keep running. When requested in the 'document ready' handler, or at the end of the document, things go bad.

    Setting the autofocus on more than one element doesn't make much sense, but the browser shouldn't crash. The JavaScript fallback may be used to provide a consistent UX for browsers that lack support for the autofocus attribute. The obvious fix for this is to remove all conflicting autofocus attributes.

    /*
    // Load event on window object: NO CRASH
    window.addEventListener('load', (e) => document.querySelector('input[name="field_1"]').focus());
    
    // DOMContentLoaded event on document object: CRASH
    document.addEventListener('DOMContentLoaded', (e) => document.querySelector('input[name="field_1"]').focus());
    */
    
    // End of document script: CRASH
    document.querySelector('input[name="field_1"]').focus();
    
    
    
    

提交回复
热议问题