Availability of Javascript variables to Vortex

Post Reply
barry.marcus
Posts: 288
Joined: Thu Nov 16, 2006 1:05 pm

Availability of Javascript variables to Vortex

Post by barry.marcus »

I'm hoping there is a way to do this...

I have a complicated legacy function written in java which, with minor tweaks, I've converted into javascript. I would like to leverage its functionality in one of our Vortex pages. The output of the function is a single string. My question is how to get the output of the function into a Vortex variable so that I can use it in the enclosing Vortex code. I see that there has been some discussion of this on the message boards in the past, but I can't put my finger on the solution, if there is one.

Here is my attempt at a "proof of concept", which is not working. Upon a click of the button, the embedded script is called. Its return value is what I would like to use in the vortex code. (The actual function is MUCH more complicated, and its return value is dependent on the value being passed to the function.) In this overly simplified example the javascript writes the return value to a textbox on the form. I'd much prefer instead for the value to be written to a vortex variable to be referenced as, say, $myReturnValue, but how to do that is what has me stumped. As you can see, referencing the value of the textbox doesn't work either (which also confuses me!)

Of course, I could translate the entire legacy function into Vortex and that would probably solve the problem. But good lord...! That's a lot of rewrite!

Thanks for your help.

<script language=Vortex>
<a name=main PUBLIC>
<LoadForm>
</a>
<a name=LoadForm PUBLIC>
<verb noesc>
<script language=JavaScript>
function myLongFunction(someValue) {
//Actual function is much more complicated and return value depends on input value
var sReturn = "My return value";
document.getElementById('resultID').value = sReturn;
}
</script>
</verb>
<form name=myform method=post action="$url">
<input type=text name=inputValue style="width:150"><br>
<input onclick="myLongFunction($inputValue)" type="button" value="Show Result" style="width:150"> <br>
<input type=text id="resultID" name=theResult disabled style="width:500"> <br>
The return value of the function is: [$theResult]<br>
</form>
</a>
</script>
User avatar
John
Site Admin
Posts: 2597
Joined: Mon Apr 24, 2000 3:18 pm
Location: Cleveland, OH
Contact:

Availability of Javascript variables to Vortex

Post by John »

Since the Vortex script is done running by the time the form is displayed it can't directly access it. You could have the javascript set the value of a hidden input field and then submit the form, at which point it is a form variable like any other.
John Turnbull
Thunderstone Software
User avatar
jason112
Site Admin
Posts: 347
Joined: Tue Oct 26, 2004 5:35 pm

Availability of Javascript variables to Vortex

Post by jason112 »

As John said, the big roadblock is not just the fact that the Javascript is not executed at the same time, it's not executed in the same location - the vortex code is run on the server, and once it's complete, the resulting html (and JS) is sent to the browser, which shows the page & parses the JS.

You could look into making an AJAX request from the page. Your initial vortex code would be "incomplete", just making the JS stuff. The browser gets & processes this page, and the JS makes a call to the server (while providing theResult contents) and vortex can do what it wishes.

It's a similar idea to the "instant search" pages; the search results depend on the result of the user's typing, which obviously isn't available when the page initially loads. So, the JS provides information to the server dynamically so the rest of the page can be "fleshed out". Your JS would be using the result of a long function instead of the result of user typing.
User avatar
jason112
Site Admin
Posts: 347
Joined: Tue Oct 26, 2004 5:35 pm

Availability of Javascript variables to Vortex

Post by jason112 »

You could also look at running the javascript directly on the server, through <exec>ing node.js, or having vortex make a request to a node.js server.

http://net.tutsplus.com/tutorials/javas ... h-node-js/

Of course, down the road of setting up an internal server to host this functionality, you could just use a Tomcat application server and make a servlet wrapper for the original Java code. :)
barry.marcus
Posts: 288
Joined: Thu Nov 16, 2006 1:05 pm

Availability of Javascript variables to Vortex

Post by barry.marcus »

Thank you! This has given me some good ideas!
Post Reply