Page 1 of 1

How to add JavaScript code on server side?

Posted: Thu May 25, 2023 4:07 am
by ppinto

Back in v2020, I could add JavaScript code to a global PHP variable in any server-side event and just add this to a client-side startup script like this:

<?php 
global $javascript_to_run;
echo $javascript_to_run; 
?>

In v2023, this is not possible, as it turns an error when generating: "Error: PHP code is not allowed in global Client or Startup Script."
How can I add JavaScript code that depends on conditions that are evaluated server-side, say in a Row_Rendering event?


Re: How to add client JS code server side (v2023)

Posted: Thu May 25, 2023 8:25 am
by mobhar

It actually depends on what exactly did you want to achieve? Why did you need to put PHP code inside the Global Client Scripts? What condition did you mean? Please explain it in more detail.


Re: How to add client JS code server side (v2023)

Posted: Thu May 25, 2023 9:39 am
by ppinto

I need that decision to be made during server-side events like Row_Rendering, for example.
I put the PHP code above in the client script just to echo a string of javascript that was previously created during the server-side events, as needed (loads of reasons for that).
Will gladly change the approach as long as the decision to add javascript code can be made server-side. Any way to achieve that?


Re: How to add client JS code server side (v2023)

Posted: Thu May 25, 2023 9:47 am
by mobhar

If you meant you want to display something from server event in client side, then you may use SetClientVar global function. For more info, please read Some Global Functions.


Re: How to add client JS code server side (v2023)

Posted: Sat May 27, 2023 2:29 am
by ppinto

Thanks for the suggestions, guys. Using SetClientVar() did it for me.
Thanks for pointing me in the right direction.