Question Đặng Phú Lộc · Mar 19, 2020

[JavaScript] Accessing server function/variable in JS file

Hi everyone,

I have a javascript function in *.js file. This function has to access to variable set by server.

I've tried setting the variable in *.js like this;

function dummy(){

var myText = '#(myText)#';

}

However it seems like the webserver doesn't understand this coding convention. The js file sent back to client set myText in dummy() with the hard-value #(myText)#. It's not the value of myText variable in server side.

I know I can achieve this by putting dummy() directly in csp file. However I have to put dummy() in separated file, so that other files can use it.

My current work-around is putting dummy in a separated *.csp file. This file acts as common javascript library for my product. And other files which want to use dummy() will have to csp:include it.

My questions are:

1. Is there any way to access server side variable from function put in *.js file?

2. Is there any drawback with my work-around (by putting dummy in pseudo javascript-csp file)?

Comments

Eduard Lebedyuk · Mar 19, 2020

I would really recommend moving to REST. With InterSystems products it's very easy. There are many articles available here on community about REST implementation.

0
Đặng Phú Lộc  Mar 19, 2020 to Eduard Lebedyuk

Thank you for your anwser. Moving to REST now is not an option as the product is already deployed in live server.

0
Julius Kavay · Mar 19, 2020

JavaScript, as your code shows, is compiled and executed on client side (in the browser).


The part of the code  #()#  you use is CSP (CacheServerPages), which is compiled  and prepared by the server for  execution by client.

Hence, you can't use the #()# syntax in a pure JavaScript code.

0