Set Axure variable via Javascript

Hi all,

I’m sucessfully running lots and lots of Javascript within Axure and its working very well. I’m even able to pull values from Axure variables into Javascript variables via a simple Javascript call like: var myVar = parseInt(’[[axureVar]]’);

One thing I’m not able to do (or haven’t figured it out yet) is how to set an Axure variable via Javascript. I’ve tried different variations of this within Javscrtipt in Axure RP 7 but can’t get it to work: $axure(’@axureVar’).value(5);

Any ideas?

sure :wink:

to set a global axure variable from javascript you need to use the correct axure function… setGlobalVariable.
the function is:

$axure.setGlobalVariable(“yourVariableName”, “yourVariableValue”);

this will work from a plugin or somehow included js-file.

if you want to set a variable value from axure… what does not make so much sens. you have to use:

javascript:void($axure.setGlobalVariable(“yourVariableName”, “yourVariableValue”))

you can also use the axure expression for reading a variable value:

getGlobalVariableValue(“yourVariableName”)…

enjoy

1 Like

Gregor, have you ever been able to actually use that function? This function always returns nothing because of what I imagine to be a scope issue.

Thank you Gregor!!! It works perfectly, much appreciated :smiley: :bow:

@nkrisc

i dont use this so much. when i implemented it the last time it was in a demo file i did in axure 6.5. 6.5 was a little critical with ‘’ and “” and “” so i escaped it.

javascript:void(alert%28GetGlobalVariableValue%28%27test%27%29%29);

this did work in axure 6.5. but no. no success in axure 7…

javascript:void(alert(’[[test]]’));

does it well in axure 7.

@Gregor

Ah, I see now that you mean using it within Axure via Open Link in Current window… etc., Axure will correctly evaluate [[]] expressions before running the javascript. This isn’t what I meant but is new to me and still very good to know!

What I meant was that trying to use the getGlobalVariable method of $axure from an external script (or the console if you’re playing around) always returns undefined. setGlobalVariable works fine from an external script, however.

I was hoping to be able to read and write Axure variables entirely from an external script so I could, for example, invoke a function to add commas to arbitrary numbers stored in variables before setting text on a widget via that variable’s value.

I suppose instead one could pass it as such:

javascript:void(myFunction(’[[myAxureVariable]]’,'myAxureVariable));

function myFunction(val,variableName){
$axure.setGlobalVariable(variableName, val + ‘some added stuff’)
}

This would be a useful way of processing variables and updating their values before invoking them in Axure.

i think you can… and i think you can also cause an event in axure if the updated variables should be used there.

its close to midnight here… i’ll have a try with an external js tomorrow and let you know.

I think this is actually due to a bug in doc.js.

On line 649 the value is never returned. If you step through the getGlobalVariable function it correctly gets the variable value but at the end it never actually returns that value to you so you just get undefined.

If you put this in your own JS file and load it, the getGlobalVariable function will work properly.

(function () {
$axure.internal(function($ax){
$ax.public.getGlobalVariable = $ax.getGlobalVariable = function(name) {
return $ax.globalVariableProvider.getVariableValue(name);
};
});
})();

I just discovered this bug yesterday, it was quite fun to track down.

3 Likes

nice work…

i’ll stop my effords. other stuff is working.

if so needs functions to cause an event in axure:

$axure.setGlobalVariable("test","87653"); 
$axure("[data-label=rect]").moveBy(0, 0, {}); (with data-label)
$axure('@panel').SetPanelState(2, {}, false); (with AxQuery)

As a somewhat related note,

I found another bug that might bite you when trying to set the text on something via javascript.

If you try to set the text on an Input by doing something like $axure(’@myInput’).value(“test”); it’ll throw an error that SetWidgetFormText is not defined. SetWidgetFormText is defined in expr.js but isn’t referenced correctly (apparently) from axQuery.std.js so that throws the error.

Another issue with that function is that it takes the elementIds and assumes it’s an array without checking it. This means that an ID of U5 will result in it trying to apply the text to something with ID U and with ID 5. Of course that won’t work. So I fixed this function locally as well.

You can check it out at the bottom of https://github.com/itorrey/EpiPen/blob/master/plugins/EpiPen/epi.js

The repo is something I’m currently working on to make it easier to inject JS/CSS or HTML into any Axure page and run custom JS functions by setting the value of a global variable to arbitrary JS.

well,i just see see

Oy. I’ve looked right past that before while trying to figure out why it wasn’t working. I can’t believe I didn’t notice there was no ‘return’ operator.

I went ahead and modified the base Axure files and it works fine now, we’ll see if that breaks anything!

Great work on the repo, btw. I’ll be following that.

Hi,

I am trying to access Axure global variables into Java script using below function:
getGlobalVariableValue(“txtTemp1”);

but I am getting below JavaScript Error:
SCRIPT5009: ‘getGlobalVariableValue’ is undefined;

even below function is not working:
parseInt(’[[txtTemp1]]’);

I want to do some manipulations on that variable.

So please help me out in this.

Thanks in adavance.

1 Like

getGlobalVariable() is a method of the $axure object.

$axure.getGlobalVariable('variable')

But be aware that in all current version of Axure so far, that function won’t actually return the value as it’s missing the return keyword. See iTorrey’s post above.

You’ll also need to include this bit:


$axure.internal(function($ax){
    $ax.public.getGlobalVariable = $ax.getGlobalVariable = function(name) {
        return $ax.globalVariableProvider.getVariableValue(name);
    };
});

I’m trying to set a global variable with this:
var position = “ABC”;
void($axure.setGlobalVariable([[Point1]], position));

“ABC” should be some else later, but even this is not working. The global variable remains empty. I’m testing that with putting the text of it in a label. What am I doing wrong?

Thanks!

Likely the issue is that the first argument of the setGlobalVariable function needs to be a string.

void($axure.setGlobalVariable('Point1', position));

Also double check that you’ve included the code from my post above that fixes the bug where that function doesn’t return a value.