Sie sind nicht angemeldet.

1

Montag, 5. November 2018, 12:47

Assigning urls to openurl in for loop via variables

Hi

I'm trying to create a row of buttons that take you to a different website.

I have a custom xml element in the tour.xml file:

<aptmenu name="test1" targeturl="http://www.google.com" image="aptmenu/test1.jpg"/>
<aptmenu name="test2" targeturl="http://www.iltalehti.fi" image="aptmenu/test1.jpg"/>
<aptmenu name="test3" targeturl="http://www.hs.fi" image="aptmenu/test1.jpg"/>


In the vtourskin.xml file I have an action that creates the button row. I've mostly just copied the skin_addthumbs action. I try to assign the targeturl attributes to openurl in the buttons' onclick events:

for(set(i,0), i LT aptmenu.count, inc(i), calc(layername, 'skin_aptmenu_' + i);
addlayer(get(layername));
set(layer[get(layername)],
url=get(aptmenu[get(i)].image),
keep=true, parent='skin_aptmenu',
align='lefttop',
crop=get(thumbcrop),
width=get(thumbwidth),
height=get(thumbheight),
x=calc(thumbpadding + i*thumbxoffset),
y=get(thumbpadding),
onclick=openurl(get(aptmenu[get(i)].targeturl), _self)
);
);


The image for the buttons is loaded just fine, so the attributes are read correctly, but when I click a button, the targeturl value is null. The browser tries to open localhost/null.

I've managed to deduce that this is some kind of a scope issue. When I set the scope of the action to global, define a variable with the targeturl and then use that variable in openurl it works:

for(set(i,0), i LT aptmenu.count, inc(i), calc(layername, 'skin_aptmenu_' + i);
addlayer(get(layername));

def(test, string, get(aptmenu[get(i)].targeturl));

set(layer[get(layername)],
url=get(aptmenu[get(i)].image),
keep=true, parent='skin_aptmenu',
align='lefttop',
crop=get(thumbcrop),
width=get(thumbwidth),
height=get(thumbheight),
x=calc(thumbpadding + i*thumbxoffset),
y=get(thumbpadding),
onclick=openurl(get(test), _self)
);
);

However the variable is overwritten in each loop and every button opens the last targeturl in the aptmenu elements. Apparently variables defined inside a loop aren't local to that loop? How do I get the targeturl attributes correctly to openurl?

2

Montag, 5. November 2018, 12:52

Hi,

that part is the problem:

Quellcode

1
onclick=openurl(get(aptmenu[get(i)].targeturl), _self)


With that code the 'i' variable will be resolved when clicking the element.

Here a better version - here the onclick string will be build directly with the targeturl value:

Quellcode

1
onclick=calc('openurl('+aptmenu[get(i)].targeturl+'), _self)')


Best regards,
Klaus

3

Montag, 5. November 2018, 12:59

That works. Thanks a lot!