Scripts ready event #173

Closed
opened 2018-08-05 12:48:07 -05:00 by Javiani · 4 comments
Javiani commented 2018-08-05 12:48:07 -05:00 (Migrated from github.com)

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!

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!
BehindTheMath commented 2018-08-05 18:46:27 -05:00 (Migrated from github.com)

Why can't you just listen to the pjax:success event?

Why can't you just listen to the `pjax:success` event?
Javiani commented 2018-08-11 10:09:18 -05:00 (Migrated from github.com)

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?

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?
BehindTheMath commented 2018-08-15 16:05:58 -05:00 (Migrated from github.com)

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.
Javiani commented 2018-08-17 21:30:05 -05:00 (Migrated from github.com)

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
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: iLoveElysia/pjax#173