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-scripts it seems that you create another script tag 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!
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-scripts` it seems that you create another `script` tag 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:success event?
Because pjax:success fires before the script of the current page is fully loaded and executed.
I know that because if I set a setTimeout function in pjax:success callback with 2 secs delay it works. So it's clearly a asynchronous issue.
I'm guessing that pjax:success fires 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 call this.onSwitch() at the end of my function, but internallly, Pjax will create another script, 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 onload event, so there's no way to know when that script is ready and executed. You know what I mean?
Hey @BehindTheMath , thanks for the reply!
> Why can't you just listen to the pjax:success event?
Because `pjax:success` fires before the script of the current page is fully loaded and executed.
I know that because if I set a `setTimeout` function in `pjax:success` callback with 2 secs delay it works. So it's clearly a asynchronous issue.
I'm guessing that `pjax:success` fires 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 call `this.onSwitch()` at the end of my function, but internallly, Pjax will create another `script`, 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 `onload` event, 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.
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
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
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
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