Darky WC
Darky WC

Reputation: 311

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?

Upvotes: 1

Views: 446

Answers (2)

SSH
SSH

Reputation: 2553

Actually you can set a variable in this manner but it has to be in a global scope not in a function scope. If you remove var val=0; from your function it should work. It's better to just return values instead of using hardcoded variable names to avoid problems with scoping, duplicate naming etc.

Upvotes: 1

DAiMor
DAiMor

Reputation: 3205

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>

Upvotes: 2

Related Questions