Using Console to debug JavaScript
 

Using Console to debug JavaScript
or any web-based code

Having looked at the console object before and not really understanding it, here is another go!

The Console is involked in the Microsoft Edge browser by pressing F12, this is probably similar in other browsers.

While the Console is a powerful tool, similar to AI, it is not really much help if you don't really know what you are doing in the first place!

To effectively use Console the coder is required to add addtional coding to write messages to the Console. This can be seen on my page on the Let keyword

PHP code

I have authored pages on how PHP scripts can be debugged. In many cases a PHP page will just crash-and-burn so the use of Console will be of little use.

In a similar manner to pages with Javascript any errors in the processing can be debugged in Console. However, these bugs are more likely to be what variables etc are being set correctly or otherwise and not syntactical errors. It is the syntactical errors that will cause the PHP page to fail.

Top

Hiding the mobileheader image

When debugging a page that has code that echoes messages to the console I have found that hiding the iamge on both large and medium resolutions works best for me. More text is shown on the screen so that I can see more of what I trying to do.

An API call on this page

Below is some code that makes a API call to retrieve the current IP address.

This code will not be seen on the screen and the result can be seen in the console

The API returns a JSON Object. I have added a console.log() entry to echo the JSON object.

Using Console to debug PHP

This is similar to the use with Javascript as you need to echo a Javascript into the PHP code:

A simple PHP file

<?php
$message = "Hello from PHP!";
echo "<script>console.log('" . addslashes($message) . "');</script>";
?>

I haven't actually done this yet.

Added to write_ip.php

Console seems to be of secondary use as the PHP will still crash and burn if there are errors in the PHP.

Links

Link to PDF Disabled

If you require a copy please email tempusfugit.me.uk

Top

References - a note on these

  • The Console Object - https:// www.w3schools.com/jsref/ obj_console.asp
  • Console log() - https:// www.w3schools.com/jsref/ met_console_log.asp
  • PHP addslashes() Function - https:// www.w3schools.com/php/ func_string_addslashes.asp - This is so "special" characters are handled properly - ie in a database query

Site design by Tempusfugit Web Design -