简体   繁体   English

$(document).ready 不会在 iOS 上触发

[英]$(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.上面的代码在桌面和 Android 设备上运行良好,但在 iOS 设备上运行时,语句 #2 不起作用。 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.只是为了测试,我通过复制/粘贴在控制台上执行了语句 #2,它按预期运行 - 这不仅会自动工作,页面加载完成。

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 PS.:我在 iOS 设备上遇到错误 - 在 iPhone 上使用 Safari 进行测试,并使用iOS Webkit Proy在带有 Chrome 远程的 Windows 10 上进行调试

I've found the problem: Somewhere in my code, i had a function with a predetermined value, and only Safari thrown an error.我发现了问题:在我的代码中的某个地方,我有一个具有预定值的函数,只有 Safari 抛出了错误。 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.我遇到了同样的问题,但我发现这是因为我在脚本包含的末尾添加了“async defer”,这导致该函数在可用之前被调用 - 导致所有后续 javascript 函数失败。 Not sure why this only happened in Safari and not Chrome/FF, but fun stuff.不知道为什么这只发生在 Safari 而不是 Chrome/FF,但有趣的东西。

<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.删除“异步延迟”部分(谷歌告诉我添加以提高页面加载速度)解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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