Edmonds Commerce Logo
  • home
    • blog
  • ecommerce
    • product catalogue
    • order processing
    • customer services
    • stock control
    • human resources
    • management information
  • development
    • oscommerce
    • php
    • mysql
    • open source
    • performance tuning
  • design
  • marketing
  • contact us
    • pricing

Edmonds Commerce Blog

Freelance PHP Ecommerce and SEO Developer in the UK

Latest Posts

Zend_Dojo Dijits and Numeric ID’s
Happy New Year from Edmonds Commerce
Easy Security for PHP Scripts
Secure and Insecure Contents in HTTPS

Most Popular Posts

PHP Email Attachment Function Freelance osCommerce UK Ultimate osCommerce Checkout - Fast and Friendly PHP : Dead Easy Excel Export

Javascript Debugging

November 18th, 2008
Read More javascript

When creating Javascript, it can be useful to alert out the values of various strings or arrays. You can also use this to confirm if certain parts of loops or if statements are being reached. However - having loads of alert statements in your Javascript can be messy and time consuming to manage.

For this reason I use this Javascript debug function. I use a PHP variable $debug to control whether or not this Javascript debug function outputs anything or not. If enabled, it will create an alert with info on the variable, array or whatever that has been passed to it.

If the $debug PHP variable is not set to true, the dbug function simply returns as soon as it is called - does nothing basically.

Here is the Javascript function

PLAIN TEXT
JAVASCRIPT:
  1. // for debugging only
  2. function dbug(arr,level) {
  3.     <?php
  4.     if(!$debug){
  5.         echo 'return;';
  6.     }
  7.     ?>
  8.     var dumped_text = "";
  9.     if(!level) level = 0;
  10.  
  11.     //The padding given at the beginning of the line.
  12.     var level_padding = "";
  13.     for(var j=0;j<level+1;j++) level_padding += "    ";
  14.     if(typeof(arr) == 'object') { //Array/Hashes/Objects
  15.         for(var item in arr) {
  16.             var value = arr[item];
  17.             if(typeof(value) == 'object') { //If it is an array,
  18.                 dumped_text += level_padding + "'" + item + "' ...\n";
  19.                 dumped_text += dump(value,level+1);
  20.             } else {
  21.                 dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  22.             }
  23.         }
  24.     } else { //Stings/Chars/Numbers etc.
  25.         dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
  26.     }
  27.     alert(dumped_text);
  28. }

To use the function, first of all (assuming you are using PHP) you need to declare the $debug variable at the top of your script and set it to true:

PLAIN TEXT
PHP:
  1. <?php
  2. $debug = true;

Then in your Javascript simply use dbug(var) whenever you want something outputted.

For example:

PLAIN TEXT
JAVASCRIPT:
  1. //Calling the function...
  2. function init() {
  3. var arra = new Array("Hloo",'s',23,23.1,"Hello World");
  4. var assoc = {
  5.  "val"  : "New",
  6.  "number" : 14,
  7.  "theting" : arra
  8. };
  9.  
  10. dbug(assoc);
  11. }

This was adapted from the Dump function available here

Feed | Respond | Trackback

Leave a Reply

  • RSS Feed
  • Categories

    • apache
    • barcode
    • business
    • creloaded
    • curl
    • customer services
    • debugging
    • ecommerce
    • edmondscommerce
    • email
    • excel
    • firefox
    • flash
    • gd
    • graphs
    • hosting
    • icecat
    • internet news
    • javascript
    • link building
    • linux
    • magento
    • management
    • mod_rewrite
    • mysql
    • oscommerce
    • php
    • plesk
    • product catalogue
    • product feed
    • programming
    • regular expressions
    • scraping
    • search engine optimisation
    • security
    • seo
    • spidering
    • ubuntu
    • Uncategorized
    • vps
    • web design
    • web development
    • Windows
    • xampp
    • zip
  • Archives

    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
  • Tags

    addons advanced adverts blackhat blocking css development directories find firefox google hosts file html javascript keywords links msn mysql myths operators oscommerce paid links paid placement performance php ppc reciprocal linking replace screen scraping search engine optimisation security seo serp speed spider spidering tuning user friendly vista web web design web developer
  • Random Posts

    • Faster File Browsing with Tabs for Ubuntu
    • Using cURL with XAMPP
    • Essential Search Engine Optimisation Research Tool
    • SEO Service Providers - What to Check For:
    • PHPNW08 - PHP Conference Up North!!
    • Block Google Analytics Cookies to Stop Staff Contaminating your Data
    • PHP Random Sleep Function with Flush
    • Zend_Dojo Dijits and Numeric ID's
    • osCommerce Password Reset Using phpMyAdmin
    • Who Needs Photoshop? PHP GD Images and Your Online Store

Edmonds Commerce related questions? Send us a message or call us on 0844 357 0201.

Freelance PHP Web Design UK Commercial Web Design