Scripts ready event #173
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Hi! First of all, thanks for this awesome lib!
Now, I'm facing a issue where I need to call my library after the scripts of the new page are loaded.
But as I saw in your
execute-scriptsit seems that you create anotherscripttag and then remove that... So there's no way I can tell that scripts are ready so I can call my update function.I'm wondering if there's any way to know when the scritps of the next page are loaded.
Thanks!
Why can't you just listen to the
pjax:successevent?Hey @BehindTheMath , thanks for the reply!
Because
pjax:successfires before the script of the current page is fully loaded and executed.I know that because if I set a
setTimeoutfunction inpjax:successcallback with 2 secs delay it works. So it's clearly a asynchronous issue.I'm guessing that
pjax:successfires only when html is ready.I can solve this asynchronous issue for html and css cases, by using the
Pjax.switches. But the Js is a particular case, because I'm forced to callthis.onSwitch()at the end of my function, but internallly, Pjax will create anotherscript, fetch new js content, execute it and then it will remove that script element.That is a problem because I can't access that script element and set a
onloadevent, so there's no way to know when that script is ready and executed. You know what I mean?It's a bit hard to test this, but it seems that inline code is run immediately and synchronously, so that would finish executing before
pjax:success. Code in an external file is executed afterwards, possibly in the next tick, maybe because it has to be fetched first.Are you using an external file?
If yes, I would suggest adding a global callback, and have the external code call that when it's finish.
Hi @BehindTheMath,
It seems that this kind of issue is not often as I thought, so I can solve that by adding that global callback or making some pub/sub notifications.
Thanks for your help man.
Cheers