简体   繁体   English

等待内联js加载/完成

[英]Wait for inline js to load/finish

I have both inline js and external js on website.我在网站上有内联 js 和外部 js。

I have problem because in external js I need variable that was is defined in inline js, but to my surprise external js happens to soon and yells at me that said variable is not defined.我有问题,因为在外部 js 中,我需要在内联 js 中定义的变量,但令我惊讶的是,外部 js 很快就会发生,并对我大喊说该变量未定义。

It works if I use $(document).ready() , yet that is too late for me.如果我使用$(document).ready() ,它会起作用,但这对我来说为时已晚。 I need something similar but less "powerful", waiting only for inline js to finish, not waiting for DOM to finish.我需要一些类似但不那么“强大”的东西,只等待内联 js 完成,而不是等待 DOM 完成。

Is there function that waits for inline js to finish, or at least for variable to be defined?是否有等待内联 js 完成的函数,或者至少等待定义变量?

Only thing i could think of was using timer that would periodically wait if needed variable is defined, but that is obviously ugly solution.我唯一能想到的是使用计时器,如果定义了需要的变量,它会定期等待,但这显然是丑陋的解决方案。

There's no separate event for "inline javascript ready" because a script tag is just a tag and, as such, it can appear at any position within a document. “inline javascript ready”没有单独的事件,因为脚本标签只是一个标签,因此,它可以出现在文档中的任何位置。 Even at the very end.即使在最后。 So, it seems to me, your only two options (one really) are to:所以,在我看来,你唯一的两个选择(一个真的)是:

  1. Use DOM ready event使用 DOM 就绪事件
  2. Reorganize your code to remove this load time dependency.重新组织您的代码以删除此加载时间依赖性。 Which makes me think: isn't including link to your external JS just after your inline block enough?这让我想到:您的内联块之后包含指向外部 JS 的链接还不够吗?

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

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