GetSimple Support Forum

Full Version: Some javascript problems in admin area
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I recently discover a problem with 3.1B version. In admin area, on file admin/template/header.php, this piece of code generated an uncaught exception :
Code:
var obj = jQuery.parseJSON('<?php echo get_api_details(); ?>');
if(obj.status != 1) {
    $('a.support').parent('li').append('<span class="warning">!</span>');
    $('a.support').attr('href', 'health-check.php');
}

We use getsimple for a project hosted on sourceforge. On this host service, all outbound communications are forbidden (http://sourceforge.net/apps/trac/sourcef...nnectivity) and the PHP function "get_api_details()" returns an empty string.

As mentionned in jquery docs http://api.jquery.com/jQuery.parseJSON/, the parseJSON method returns null if an empty strings is passed in parameter, so reading obj.status directly after calling parseJSON can end with an error. This error causes many Javascript codes to not be executed in the rest of page (especially with the i18n_navigation plugin).

To prevent these king of problem, it is possible to control that obj is different from null before reading its status property. Moreover, as mentionned in jQuery doc, parseJSON method can throws exceptions if malformed JSON is gave as parameter. So I would like to suggest this code to replace the one mentioned above :
Code:
try {
    var obj = jQuery.parseJSON('<?php echo get_api_details(); ?>');
    if(obj != null && obj.status != null && obj.status != 1) {
        $('a.support').parent('li').append('<span class="warning">!</span>');
        $('a.support').attr('href', 'health-check.php');
    }
} catch(error){
    //Something to do ?
}