I have a script "selfexec.js" which does few things:
我有一个脚本“selfexec.js”,它做了一些事情:
!function(){ A = {init : init}}()
I basically use this script as a plugin that can be loaded from other sites.
我基本上使用此脚本作为可以从其他站点加载的插件。
So, from Site B, I load this function and then want to call the method A.init but A is always coming out as undefined as its getting executed even before the anonymous function is loaded.
所以,从站点B,我加载这个函数,然后想要调用方法A.init但是A总是在未加载的情况下出现,因为它甚至在加载匿名函数之前就已经执行了。
I tried to attach it to the window but still no luck
我试着将它贴在窗户上,但仍然没有运气
Anyone has better approach of doing this? Any help would be appreciated
有人有更好的方法吗?任何帮助,将不胜感激
1 个解决方案
#1
0
it seems like the issue is that you are trying to use A variable before it was initialized by 3rd party script. So the order is matter here: load 3rd party script before and start use it after. Also I personally like an approach with putting calls to the queue (similar approach is used even for google analitics). read about it more here
似乎问题是你在第三方脚本初始化之前尝试使用A变量。所以这里的顺序很重要:之前加载第三方脚本并在之后开始使用它。此外,我个人喜欢将呼叫调用到队列的方法(类似的方法甚至用于谷歌分析)。在这里阅读更多
https://coderwall.com/p/2wmqhw/load-3rd-party-js-asynchronously-initialize-queue-replace-queue
so, you will need to define a way how your client should load code from your site and all methods will 100% be executed after 3rd party code was loaded and initialized.
因此,您需要定义客户端如何从您的站点加载代码的方式,并且在加载和初始化第三方代码后,所有方法都将100%执行。
#1
0
it seems like the issue is that you are trying to use A variable before it was initialized by 3rd party script. So the order is matter here: load 3rd party script before and start use it after. Also I personally like an approach with putting calls to the queue (similar approach is used even for google analitics). read about it more here
似乎问题是你在第三方脚本初始化之前尝试使用A变量。所以这里的顺序很重要:之前加载第三方脚本并在之后开始使用它。此外,我个人喜欢将呼叫调用到队列的方法(类似的方法甚至用于谷歌分析)。在这里阅读更多
https://coderwall.com/p/2wmqhw/load-3rd-party-js-asynchronously-initialize-queue-replace-queue
so, you will need to define a way how your client should load code from your site and all methods will 100% be executed after 3rd party code was loaded and initialized.
因此,您需要定义客户端如何从您的站点加载代码的方式,并且在加载和初始化第三方代码后,所有方法都将100%执行。