0

I have a PHP script that logs informational and error messages to a database table. I want to build a javscript singleton to use to call this script with parameters based on the info or error that I need to log. The PHP script checks to see if the logonly parameter is set, and if so, only logs the values, if it is not set, it logs the error and also emails me that it wrote to the log so I can check it immediately when I get the email.

Here is the javascript singleton:

[BrowserDetect is a class for browser detection: http://www.quirksmode.org/js/detect.html]

var NATools = {
  var browser    = BrowserDetect.browser;
  var browserVer = BrowserDetect.version;
  var browserOS  = BrowserDetect.OS;
  var AJAX_ERROR_LOG_URL      = '/ajax/log_ajax_error.php';

  DebugLog: function(log_only, url, msg, browser, browserVer, browserOS, jqXHR, exception) {
    var logurl = AJAX_ERROR_LOG_URL;
    if(log_only) {
      var logurl += '?logonly=yes&url='+url+'&browser='+browser+'&browserVer='+browserVer+'&browserOS='+browserOS;
    } else {
      var logurl += '?status='+jqXHR.status+'&object='+jqXHR+'&error='+jqXHR.responseText+'&expmessage='+exception.message+'&url='+url+'&browser='+browser+'&browserVer='+browserVer+'&browserOS='+browserOS;
    }
    $.ajax({
        async: true, 
        type:'POST', 
        url: logurl, 
        dataType: 'json'
    }); // $.ajax
  }; // DebugLog
}; // NATools

Here is how it would be called in an informational situation:

// url is preformed in this situation
NATools.DebugLog(true, url, "Good Entry", browser, browserVer, browserOS);

Here's how it would be called in an ajaxError situation:

$(document).ajaxError(function(e, jqxhr, settings, exception) {
  var ajax_url = settings.url;
  NATools.DebugLog(false, ajax_url, "Error Entry", browser, browserVer, browserOS, jqxhr, exception);
}); // ajaxError

My question is, do I need to pass NULLS for the jqXHR and exception parameters in the informational situation? Or is it possible to set default optional parameters here?

MB34
  • 4,210
  • 12
  • 59
  • 110

1 Answers1

1

Yes, the "default value" for all JavaScript parameters is undefined. So check for undefined using one method or another:

if(someParam === undefined) {
    someParam = 'default value';
}
Community
  • 1
  • 1
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • I was really asking if it was required to do that since I am not using them if log_only is true. If log_only is true and I don't pass a jqXHR or exception object, I am not using them so I don't care. I already know how to determine if it is undefined. I want to make this as usable as possible. – MB34 Jan 24 '12 at 20:13
  • @MB34: No, it's not required. You can safely both ignore them and not pass them. – Ry- Jan 24 '12 at 20:19