简体   繁体   English

删除“text/javascript”属性时未定义 Jquery

[英]Jquery is not defined when remove "text/javascript" attribute

Scenario that works:有效的场景:

  • I have an APP_FRAMEWORK.JS file that was generated through a WEBPACK , including BOOTSTRAP AND JQUERY inside it.我有一个通过WEBPACK生成的APP_FRAMEWORK.JS文件,其中包括WEBPACK BOOTSTRAP AND JQUERY

     <script type="text/javascript" src="{{ asset('js/app_framework.js') }}" defer></script> @if(!empty($assets['js'])) @foreach ($assets['js'] as $js) <script type="text/javascript" src="{{ asset($js) }}"></script> @endforeach @endif

If I load the page as in the example, it works perfectly, without any errors in the scripts that are loaded inside that loop below.如果我像示例中那样加载页面,它将完美运行,在下面的循环中加载的脚本中没有任何错误。

My question:我的问题:

  • Why, if I remove the "text/javascript" when loading the MAIN SCRIPT (APP_FRAMEWORK.JS) the $ is not defined error starts to appear?为什么,如果我在加载MAIN SCRIPT (APP_FRAMEWORK.JS)时删除“text/javascript”, $ is not defined错误开始出现?
  • Remembering that this TYPE is no longer mandatory, scripts are loaded in the correct order and there is a DEFER !!!请记住,此TYPE不再是强制性的,脚本以正确的顺序加载,并且有一个DEFER !!!

在此处输入图片说明 在此处输入图片说明

  1. Forcing the browser not to load scripts from cache ( SHIFT + RELOAD on Mac)强制浏览器不从缓存加载脚本(在 Mac 上为SHIFT + RELOAD 在此处输入图片说明 Just by doing that, we already see that the APP_FRAMEWORK.JS is being loaded after the others.通过这样做,我们已经看到APP_FRAMEWORK.JS正在其他人之后加载。

  2. Another point, defer does not prevent other scripts from being loaded until those that have it defined carry out their process.还有一点, defer不会阻止其他脚本被加载,直到那些定义了它的脚本执行了它们的过程。 The definition of the defer attribute goes further, being: defer属性的定义更进一步,是:

    The defer attribute tells the browser to run the script only when the HTML parsing is finished. defer属性告诉浏览器只有在 HTML 解析完成后才运行脚本。 The script will be requested asynchronously, its download will be completed and, only when the analysis of the HTML document is finished, will it be executed.脚本将被异步请求,它的下载将完成,只有当 HTML 文档的分析完成后,它才会被执行。 Even if the full script download happens before the full HTML parsing, it won't run until later.即使完整的脚本下载发生在完整的 HTML 解析之前,它也不会在稍后运行。 If you have multiple script elements with the defer attribute, they will be requested in parallel and executed in the stated sequence.如果您有多个具有defer属性的脚本元素,它们将被并行请求并按规定的顺序执行。

  3. As the scripts are at the bottom of the page, there is no need to use defer , as it will never stop interpreting the HTML to download any JS.由于脚本位于页面底部,因此无需使用defer ,因为它永远不会停止解释 HTML 以下载任何 JS。

  4. The problem itself has nothing to do with type="text/javascript" , everything is related to the size of the loaded scripts, download time, in view of the order of dependency and execution.问题本身与type="text/javascript"无关,一切都与加载脚本的大小、下载时间、依赖和执行的顺序有关。

  5. If you really need to follow a JS loading order, defer must be used in all <script defer> , so that it follows the declared sequence, thus:如果你真的需要按照JS加载顺序, defer必须在所有使用<script defer> ,以便它跟随申报顺序,即:

<script defer src="{{ asset('js/app_framework.js') }}" defer></script>
<script defer src="{{ asset('js/app.js') }}"> others ...</script>

@if(!empty($assets['js']))
    @foreach ($assets['js'] as $js)
        <script defer src="{{ asset($js) }}"></script>
    @endforeach
@endif

在此处输入图片说明

Refer link: https://www.braziljs.org/p/diferencas-entre-async-e-defer参考链接: https : //www.braziljs.org/p/diferencas-entre-async-e-defer

And now comes the question that doesn't want to shut up... Why, even loading the files from the cache in the correct order, the browser displayed the message?现在来了一个不想闭嘴的问题......为什么,即使按照正确的顺序从缓存中加载文件,浏览器也会显示消息?

Has anyone heard of error cache from the browser based on those files?有没有人听说过基于这些文件的浏览器error cache Like, see that the scripts are the same, that the last execution of those scripts gave an error and already display the error automatically?就像,看到脚本是一样的,这些脚本的最后一次执行给出了错误并且已经自动显示错误?

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

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