How do you pass a Javascript variable value into Application variable?

How do you pass a Javascript variable value into Application variable?

  
How do you pass a Javascript variable value into Application variable?
Hi Robert,

A Javascript variable is a client side variable that is being stored in the client browser, while an Application variable is a server side variable that is avaiable in the scope of an OutSystems action on the server when the web request is being process. The way to pass a variable on the client to the server, is to make sure that you put it in an input in the form that will be submitted to the server.

For example, to have a Javascript variable string NameJS on your screen passed to the OutSystems local WebScreen variable text NameOS, you must do the following:
  1. Create a hidden Input in your screen (it can be a text box) and give it a name (eg. NameInput);
  2. Bind the OutSystems local variable NameOS to the NameInput;
  3. Make sure that your Javascript logic writes the value of the NameJS Javascript variable to the NameInput (remember that you have the OutSystems runtime property NameInput.Id in your input that gives you the DOM Id of the OutSystems input in runtime so that you can use it in your Javascript logic);
  4. Once the form is submitted to the server (with a button or link), you will have available the NameInput value available in the NameOS local variable. As your javascript logic is writing the NameJS value to the NameInput, this means you will have have achieved your goal - the NameOS variable is holding the value of the NameJS variable.
Kind Regards,

Daniel Lourenço
OutSystems
Hello,
sorry to dig this one up but, is there a way to do it without a submit button? In the preparation action, for example.
I tried to assign the javascript variable to some input, but after that without the button i cant seem to assign it to a specific session variable.

Thank you
Hi. I have a question about this question , please help me, thanks a lot.
description:
     I have an input widget named "test", and finished bind to a local variable, now I need to get this input widgets value.  
    If I use the Extended Properties of the input widget to set: Properties = ondblclick , Value = "alert(this.id)", it works.
    but now I need to get this widget id in a web screen in my javascript code. So how can I get this value?

Thanks & regards! 
Hi,

When you say "it works", can you describe what happens? I have entered the Extended Property as you said, and I get a message box saying 'undefined'.
To display the value of the input widget, just set the Extended Property value to  "alert(" + test.Id +".value)".

Yes , I just want to know how to reference the widget in the javascript , like your example . I've got it , thanks !
Jie -

I would suggest that you look at many of the Forge components... like the CKEditor component... for examples on how this is done.

J.Ja
Hello,

I am trying this way to assign javascript variable to outsystems variable, but I encountering problem.
I am running this javascript:

"var r = confirm(""Text"");
if (r == true) {
    " + Input.Id + ".value = 1;
} else {
    " + Input.Id + ".value = 0;
}"
When running it for the first time and clicking OK, I am getting blank value, when I am clicking second time Cancel, I am getting true, and this is previous value. What I shoud do that value won't be previous?
I had tried ajax refresh after RunJavaScript action, but then always getting blank value.
Hi Audrius,

I just copy pasted your Javascript code to the onblur extended property of my OutSystems input, and I get the correct behavior:
- whenever I move the focus away from the input, I get the popup with OK/Cancel;
- if I click OK the input value is set to 1;
- if I click Cancel the input value is set to 0.

It seems that you have the correct JavaScript code, but maybe you are using it in the wrong place?

Completing my previous post...
Just like any other action, RunJavaScript runs on the server side, after the request is submitted from the browser.
The RunJavaScript action injects JavaScript on the response, and the browser will execute and render this response.
The OutSystems variables are assigned when the request is submitted, so long before the response is returned to the browser.
In other words, when the OutSystems variable is assigned, the JavaScript code of the "RunJavaScript" action was not executed yet, so the OutSystems variable is "outdated".