javascriptintersystems-cacheobjectscript

Unable to set variable value from Intersystem Cache Server Method


I have came across a question when I am doing Intersystem Cache Server Page with Javascript.

Here is my Sample Code:

Case 1:

<script language="Javascript">
function test1(){
  var val = 0;
  #server(..Set())#;
  alert(val);
}
</script>
<script language="Cache" method="Set">
Write "val = 50;"
</script>

In this case, when the function test1() is called, the value = 0 and it is a local variable.

Case 2:

<script language="Javascript">
function test1(){
  #server(..Set())#;
  alert(val);
}
</script>
<script language="Cache" method="Set">
Write "val = 50;"
</script>

In this case, when the function test1() is called, the val = 50 and value is now a global variable.

So my questions are:

  1. How can I successfully set a value in Case 1 through Cache Server Method?
  2. Why Cache Server Method can declare a global variable in Javascript but not able to set a value to a variable?

Solution

  • You can't just generate javascript code on server side this way. You can return one value from this method and get it back in Javascript.

    <script language="Javascript">
    function test1(){
      var val = #server(..Set())#;
      alert(val);
    }
    </script>
    <script language="Cache" method="Set">
     quit 50
    </script>