[英]How to refer to a function that will be declared after script loading
In a Sitecore environment, where I CANNOT reorder the js scripts instantiated at the end of the <body>
, I need to check if one of the function ( Gigya ) in those scripts has returned a true value ( I need to check if a user is logged in or not ).在 Sitecore 环境中,我无法重新排序在<body>
末尾实例化的 js 脚本,我需要检查这些脚本中的 function ( Gigya )之一是否返回了真值(我需要检查用户是否是否登录)。
My script can be loaded ONLY before those scripts are loaded, so my script checks if a given function has a true return value before that said function has to be called.我的脚本只能在加载这些脚本之前加载,所以我的脚本在必须调用 function 之前检查给定的 function 是否具有真正的返回值。
Given the structure situation, what I always get is an error of unknown function.鉴于结构情况,我总是得到一个未知 function 的错误。
Is there a way to bypass this?有没有办法绕过这个? I mean, I have tried to attach at the body an ad-hoc script containing my check-function at the end of the loading process, but with no success.我的意思是,我试图在加载过程结束时在正文中附加一个包含我的检查功能的临时脚本,但没有成功。
Any hint?有什么提示吗? Maybe adding listeners?也许添加听众?
Approach 1方法一
Since the function you need to wait for is loaded with <script>
tags you could wrap your entire code inside a function and call that function with onload由于您需要等待的 function 加载了<script>
标签,您可以将整个代码包装在 function 中并调用 function
<script src="...gigya..." onload="functionToExecute()">
Approach 2方法二
instead of calling the functionToExecute
you could create a new synthetic event, say "gigyaloaded" and listen to it before your functions are called.您可以创建一个新的合成事件,而不是调用functionToExecute
,例如“gigyaloaded”并在调用函数之前收听它。
[ https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events ] [ https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events ]
the event should be emitted with script onload like above.该事件应该与上面的脚本 onload 一起发出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.