#1 Re: Topics related with App Builder » Checking if file exists and mixing AB and Javascript code » 2017-01-18 10:55:30

Okay thanks for the update, installed it ok. Now, hopefully last question before I can get this together. You mentioned: the Image control has an Error event ready to be used. If I give you some pseudo-code can you show me how to address/trap the error event?

SetVar "[picnumber]" "[picnumber]+1" "Number" // This advances image to show next in sequence
SetVar "[picnumber3]" "[picnumber]a.jpg" "String" // This builds the full image name.
SetVar "[ptoonpic.Image]" "[picnumber3]" "String" // Load the image we want.

[StartIf]
if [picnumber3] gives error,
    SetVar "[ptoonpic.Image]" "defaultimg.jpg" "String" // Load the default image on error
[EndIf]

Can you give me a code example how to access and use the image error event? I think once I grasp that I can put this together.

BTW- I notice where you live on your tag; I lived in Aravaca for 5 years (back in the 70's - a lot has changed!). Anyway, nice to meet someone from the old neighborhood, and hello from Oregon.

Thanks
Roger

#2 Re: Topics related with App Builder » Checking if file exists and mixing AB and Javascript code » 2017-01-17 10:29:10

Thanks for the quick reply; ok, I added the Start/End js; Below is my rough code. It runs through and checks if each image exists, then shows it in AB. When it gets to an image that doesn't exist (ie there is no 8a.jpg), I try to use javascript to re-value window.App.RootScope.picnumber3="1a.jpg" (back to first image). But when I run this, the log shows:

[w] Unresolved "D:\Spt\PuntoonApp\Puntoons_Compiled\www\8a.jpg"

And AB still sees the [picnumber3] is not changed back to the 1a.jpg (first image). Am I setting value of the window.App.RootScope.picnumber3 variable correctly? Failing that, is there a way in AB to capture the "Unresolved" error to flag when to change the value in AB itself?

Let me know if this is not clear. I am enjoying learning from your program!

Thanks
Roger

SetVar "[picnumber]" "[picnumber]+1" "Number"
SetVar "[picnumber3]" "[picnumber]a.jpg" "String"

StartJS

function checkImage(imageSrc, good, bad) {
    var img = new Image();
    img.onload = good;
    img.onerror = bad;
    img.src = imageSrc;
}


checkImage(window.App.RootScope.picnumber3, function(){  }, function(){window.App.RootScope.picnumber3="1a.jpg"; } );
EndJS

SetVar "[ptoonpic.Image]" "[picnumber3]" "String"

#3 Topics related with App Builder » Checking if file exists and mixing AB and Javascript code » 2017-01-17 00:27:12

rnshepard
Replies: 7

Hello, i am trying out AppBuilder, and trying to use a button to incrementally show images. That works fine, but when it reaches the last image available, I want it to go to the first image; the number of images will vary as I add more.

I tried using javascript in the button code to check if image exists, but it seems to ignore the script. I have the following:

(images are named 1a.jpg, 1b.jpg, then 2a.jpg, 2b.jpg, etc. (when I click on the image 1a.jpg, it loads 1b.jpg, etc.))

//This part works; just shows next image (first image is set in code in body of app).
SetVar "[picnumber]" "[picnumber]+1" "Number" // increments number of image name
SetVar "[picnumber2]" "[picnumber]a" "String" // appends 'a' to name

//following javascript used to see if image exists; this part is not recognized.
function checkImage(imageSrc, good, bad) {
    var img = new Image();
    img.onload = good;
    img.onerror = bad;
    img.src = imageSrc;
}
checkImage("images/[picnumber2].jpg"", function(){ SetVar "[ptoonpic.Image]" "images/[picnumber2].jpg" "String"; }, function(){ SetVar "[ptoonpic.Image]" "images/1a.jpg" "String"; } );

The javascript works outside of appbuilder, in a simple web page, but not in the button code area. Can you advise any simple way to verify if an image exists in folder so I can redirect to first image when image called doesn't exist?

Appreciate your help.

Roger

Board footer

Powered by FluxBB


Copyright ©2018 DecSoft. All rights reserved.