Home Forum Blog Market Events Resources
Showing results for 
Search instead for 
Do you mean 
Highlighted
Contributor
Posts: 32
Registered: ‎03-19-2014

Execute JS after view load

Hi Everyone,

 

Anyone had success with running JS after a view has loaded? I have been trying with several approaches all with no luck. I have tried window).load, $(document).ready. I've tried using the Initialised rule and tranfering expressions using data tranfer. Doesn;t matter which approach I use the JS will always fire before the data from view has loaded. I need to fire after becuase I want use a selector on a list view. Thanks in advance

Moderator
Posts: 648
Registered: ‎08-27-2014

Re: Execute JS after view load

Hi MrSafe,

 

Have you tried to transfer your script after initializing the form/view?

you have more than one event for the view (i.e. Initialize, Initialized), try to perform the transfer in the initialized event.  

 

Please post the script you're trying to execute if the above didn't help

Best Regards,
------------------------------------------------
Mustafa Al-Barghouthy
Contributor
Posts: 32
Registered: ‎03-19-2014

Re: Execute JS after view load

Hi Mustafa,

 

Thanks for the reply, I have tried the data transfer in the initialized rule. Currently I'm not trying anything fancy just an alert to see when it's firing. You can try it yourself quite quickly with a datalabel and a view. At first I thought it was because I put the Data Label in the form the view was hosted in, so I moved the label into the view. I have since come to the notion that I have to wait for the Ajax calls to finish before firing the JS, so I have tried using .ajaxStop and .ajaxComplete. Whilst these definately fire at different times, they still don't allow all list views to load before firing or it's running async and doesn;t care! The goal here is to be able to hit a view element with a selector, but if it's not loaded it does not exist at that moment and therefore fails.

 

Moderator
Posts: 648
Registered: ‎08-27-2014

Re: Execute JS after view load

Hi,

 

I understand what you mean, tbh I am not sure how to synchronize this using scripts.

I had a similar problem a while ago and I had to populate a data label after a certain call then added a rule to fire the script when the data label is changed.

 

 

Best Regards,
------------------------------------------------
Mustafa Al-Barghouthy
Contributor
Posts: 32
Registered: ‎03-19-2014

Re: Execute JS after view load

Hi Mustafa,

 

Thanks again for the brain storm,

 

I achieved what I wanted if not all together satifactorily.

 

I was trying get around the fact k2 does not support controls being displayed when not in edit mode. I have a list view of bits that render true or false when not in edit and render as check boxes when in edit mode. I wanted to make the view a bit more friendly by subsitutng the words true and false with images.

 

I had to use a setTimeout (Not great) to substitute after the form loaded, I then used 2 data transfer rules and the same JS without the onload and setTimeout wrappers in another expression, this was set to fire on the list item changed event.

 

This seems to work very well, however, I'm concerned about the settimeout not firing if the page takes longer than the setTimeout Threshold to fire.

 

Thanks again