Javascript variable in keyword modifier


(Mitch Kerry) #1

**Matrix Version:**5.3.1.1

Hi all

Probably an easy one but not sure how I’ve done this in the past.

Say I have a variable in javascript and I want to use that in a keyword modifier…how do I get it to ‘parse’?

e.g. I’m pulling in a number string from a URL query and I want to use that as an asset id in a modifier

I would try:
var theVar = “12345”;
var theAssetName = “%globals_asset_assetid:” + theVar + “^as_asset:asset_name%”

But this just comes back as %globals_asset_assetid:12345^as_asset:asset_name% and not the asset name.

What’s the best way to do this?

Thanks


(Peter McLeod) #2

Hi

I’m assuming this js in running in the browser (rather than server side) - if so then you would need to just use keyword formatting by itself to get the desired result, as the keywords are evaluated by matrix then returned to the browser where the JS is executed.

If the value 12345 was in a query parameter in the URL called ‘x’ (http://domain.com?x=12345) then could use something like:

%globals_get_x^as_asset:asset_name%

Not sure if this helps.

Thanks
Peter


(Mitch Kerry) #3

Thanks Peter…yes in the browser

For the URL use case it works fine…but in general, if the variable is from some other function, how can get it evaluated by matrix?

thanks!


(Peter McLeod) #4

Hi

If you want to do it from js running in the browser, you would need to make a call back to the server…

Eg: you could have an asset thats sole purpose was to get an id via a query parameter, convert the keyword and output the asset name.

A simple implementation would be a standard page with a design/layout setup to just return plain text - its content would be:

%globals_get_x^as_asset:asset_name%

Then use an ajax call to send the id to it eg if using jquery:

$.get("http://site.com/yourpage?x=12345",function(data) {
  
	// use the page name that is returned in the 'data' parameter 

});

There are other ways that you could go about this, but it would depend on the specifics of your js application - eg in some cases you might pre generate data and load it with the page, or you might look into using the JS api for more complex needs.

Thanks
Peter