URL Test From Javascript

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

tom.smith
Ebase User
Posts: 24
Joined: Thu May 09, 2013 2:54 pm

URL Test From Javascript

#1

Postby tom.smith » Fri Dec 06, 2013 3:18 pm

Hi

I'm trying to get a JS script within ebase to test a URL to see of an image is available. Essentially they are photos of colleagues, but not all colleagues have photos. If they have a photo then I'd like to assign the URL to a field value, if they don't have a photo then I'd like to assign a default graphic.

I have tried several versions of code from forums other than ebase (not being able to locate anything via the search facility) but so far have been unable to get the result that I want.

I've tried the following code:

Code: Select all

var urlTest = new XMLHttpRequest();
urlTest.open&#40;'GET', 'http&#58;//<url>', true&#41;;
urlTest.send&#40;&#41;;
fields.testField.value = urlTest.status;

This returns an error:

Code: Select all

Error&#58; ReferenceError&#58; "XMLHttpReques" is not defined.

The URL is pointing to a file saved on the same server as the ebase installation is running on, so there aren't any issues with trying to get XMLHttpRequests to work X-domain.


I've also tried the following piece of code:

Code: Select all

$.ajax&#40;&#123;
    url&#58;'http&#58;//<url>',
    type&#58;'HEAD',
    error&#58;function&#40;&#41; &#123;

        // Contact does not have an available photo, so use the default
        fields.CONTACT_PHOTO.value = "NoPhotoAvailable.png";
    &#125;,
    success&#58;function&#40;&#41; &#123;

        // Contact does have an available photo, so use that
        fields.CONTACT_PHOTO.value = fields.USERGUID.value.concat&#40;".jpg"&#41;;
    &#125;
&#125;&#41;;

This displays the error:

Code: Select all

Error&#58; ReferenceError&#58; "$" is not defined.

Any help would be greatly appreciated.

Thanks in advance

Tom
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#2

Postby Jon » Fri Dec 06, 2013 4:01 pm

Tom,

There seems to be a bit of confusion whether your Javascript code is running on the client or the server. If your images are served locally i.e. from the server's file system, it's probably easier and faster to check whether the file exists on the server as opposed to checking a URL.

Here's an example - this is server-side Javascript and needs to be executed from an Ebase event such as the before page event:

Code: Select all

var webappBase = FileServices.getWebAppRootFilePath&#40;&#41;;
var pathName = webappBase + FileServices.getSeparator&#40;&#41; + "images" + FileServices.getSeparator&#40;&#41; + fields.IMAGE_NAME.value;
if &#40;FileServices.existsFile&#40;pathName&#41;&#41;
&#123;
	event.owner.addErrorMessage&#40;"Exists"&#41;;
&#125;
else
&#123;
	event.owner.addErrorMessage&#40;"Doesn't exist"&#41;;
&#125;
The first line gets the path to the base of the web app - something like /UfsServer/tomcat/webapps/ufs. The rest of the code checks whether the file exists in the images folder.

Let me know if you still need to do the URL test - there is a way to do this with server-side Javascript - I'd need to look it up.

Regards
Jon
0 x

tom.smith
Ebase User
Posts: 24
Joined: Thu May 09, 2013 2:54 pm

#3

Postby tom.smith » Fri Dec 06, 2013 4:13 pm

Hi Jon

Thanks for that - worked a treat.

I was trying to use a URL check just in case we decide to migrate the photo cache off of the local server onto one of our web servers, but this does the job just fine.

Cheers!
0 x


Who is online

Users browsing this forum: No registered users and 26 guests