简体   繁体   中英

$(document).ready not firing on iOS

I have the following code on my page:

<script src="//path.to/jquery.min.js"></script>

<script>
 /* Statements #1 - Defining some variables... */
</script>

<script>
$(document).ready(function () {
 /* Statements #2 - Doing some stuf... */
});
</script>

The above code runs well on Desktop and Android devices, but when running on iOS devices the Statement #2 doesn't work. I've checked the console, and there are no errors.

Just for testing, i've executed Statements #2 on console by copy/paste and it runs as expected - This don't work only automatically, with page loading complete.

PS.: I got the error on iOS devices - Testing with Safari on iPhone and debugging on a Windows 10 with Chrome remote using iOS Webkit Proy

I've found the problem: Somewhere in my code, i had a function with a predetermined value, and only Safari thrown an error. So, i'm using this:

function myFunction(parameter) {
    /* Coll stuf here... */
}

Instead this:

function myFunction(parameter = undefined) {
    /* Broken code... */
}

I had the same issue, but I found that it was because I had added 'async defer' to the end of the script include, which caused that function to be called before it was available - causing all subsequent javascript functions to fail. Not sure why this only happened in Safari and not Chrome/FF, but fun stuff.

<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.1/dist/jquery.validate.min.js" async defer></script>

<script>
  $(document).on('ready', function() {
    $("#myForm").validate();
  });
</script>

Removing the 'async defer' part (that Google told me to add to increase page load speed) fixed the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM