头部中的异步javascript仅在刷新页面后重复[重复]

时间:2022-09-15 08:48:29

This question already has an answer here:

这个问题在这里已有答案:

i'm trying to load javascript asynchronous in the head section. After page loading in developer console i have error that "jQuery is not defined". After one or two refreshes scripts load and work perfectly. Without "async" inside script tag also works. Why this happens and is there any way to fix this. Thanks in advance

我正在尝试在head部分加载javascript异步。在开发人员控制台中加载页面后,我有错误“jQuery未定义”。经过一两次刷新脚本加载并完美运行。脚本标记内没有“异步”也可以。为什么会发生这种情况,有没有办法解决这个问题。提前致谢

3 个解决方案

#1


0  

As Dellirium said, JQuery needs to be placed before the javascript code that requires it. You could try placing the JQuery in the <head> and the script you're running at the end of the body tag <body>.

正如Dellirium所说,JQuery需要放在需要它的javascript代码之前。您可以尝试将JQuery放在中,并在body标签的末尾运行脚本。

#2


0  

You could add a function to be executed when jQuery is loaded.

您可以添加一个在加载jQuery时要执行的函数。

<script src="jquery.js&callback=start"</script>

In which &callback=start executes the start() function in your actual javascript file, which you can use to wrap your code around.

其中&callback = start在您的实际javascript文件中执行start()函数,您可以使用它来包装代码。

function start() {

    /* your code */

}

The reason jQuery couldn't be found the first times is because your browser caches the jQuery when it is fully loaded. It has to load the entire file the first time you actually visit or force-reload the page.

第一次找不到jQuery的原因是因为你的浏览器在完全加载时缓存jQuery。它必须在您第一次实际访问或强制重新加载页面时加载整个文件。

#3


0  

When you use async on a script tag, the script can execute in any order, at any point during the page load, which is why your scripts work only sometimes. You trade away knowing the order of execution for slightly faster page loads. This usually requires that the scripts you load do not depend on each other or an elaborate loading scheme - usually using one callback function that runs without async and invokes your code when dependencies report they're available.

在脚本标记上使用async时,脚本可以在页面加载期间的任何时刻以任何顺序执行,这就是为什么脚本有时只能运行。你知道执行的顺序,以便稍微加快页面加载。这通常要求您加载的脚本不依赖于彼此或精心设计的加载方案 - 通常使用一个没有异步运行的回调函数,并在依赖项报告它们可用时调用您的代码。

You should consider if async is really the right option for your setup.

您应该考虑异步是否真的是您的设置的正确选项。

#1


0  

As Dellirium said, JQuery needs to be placed before the javascript code that requires it. You could try placing the JQuery in the <head> and the script you're running at the end of the body tag <body>.

正如Dellirium所说,JQuery需要放在需要它的javascript代码之前。您可以尝试将JQuery放在中,并在body标签的末尾运行脚本。

#2


0  

You could add a function to be executed when jQuery is loaded.

您可以添加一个在加载jQuery时要执行的函数。

<script src="jquery.js&callback=start"</script>

In which &callback=start executes the start() function in your actual javascript file, which you can use to wrap your code around.

其中&callback = start在您的实际javascript文件中执行start()函数,您可以使用它来包装代码。

function start() {

    /* your code */

}

The reason jQuery couldn't be found the first times is because your browser caches the jQuery when it is fully loaded. It has to load the entire file the first time you actually visit or force-reload the page.

第一次找不到jQuery的原因是因为你的浏览器在完全加载时缓存jQuery。它必须在您第一次实际访问或强制重新加载页面时加载整个文件。

#3


0  

When you use async on a script tag, the script can execute in any order, at any point during the page load, which is why your scripts work only sometimes. You trade away knowing the order of execution for slightly faster page loads. This usually requires that the scripts you load do not depend on each other or an elaborate loading scheme - usually using one callback function that runs without async and invokes your code when dependencies report they're available.

在脚本标记上使用async时,脚本可以在页面加载期间的任何时刻以任何顺序执行,这就是为什么脚本有时只能运行。你知道执行的顺序,以便稍微加快页面加载。这通常要求您加载的脚本不依赖于彼此或精心设计的加载方案 - 通常使用一个没有异步运行的回调函数,并在依赖项报告它们可用时调用您的代码。

You should consider if async is really the right option for your setup.

您应该考虑异步是否真的是您的设置的正确选项。