When I develop in the web, there are plenty of times that I want to see information about where I am in the program or what is going on in the Javascript when I load the page. I use the Console to watch updates but it can be difficult to read when you have possibly hundreds of log statements.
Usually you can just use the standard console.log function:

> console.log("Some information");
Some information
or 

> console.debug("Some information");
Some information

or 

> console.info("Some information");
Some information

You also get the line number and file that the output comes from in the console.

But did you know that you can use the following:

console.clear()
console.error(object)
console.table(array)
console.warn()

Let’s say you want to clear the current console. You can place in your code, or use it within the console itself.

console.clear();

Or you can get color coded lines with the warn or error functions:

> console.warn("This is a warning");
► This is a warning
> console.error("Error: Danger");
Error: Danger
And if you need to display an array that and want a better layout use console.table():
> console.table(['a','b','c']);
(index) Value
0 “a”
1 “b”
2 “c”
► Array(3)

Kick off Chrome Debugging

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Browsers, Javascript, Scripting, Web Design | No comments yet.

I was talking to a friend of mine on the phone the other day and we were talking about how debugging in Chrome was sometimes frustrating. We both had some ideas on how that could have been made better for each of us.

While you can load your page, find the line of code, and add a set of breakpoints. He let me know kicking off the debugger in a certain part of your code just by adding the following line of code where you want the code to stop:

debugger;

Once this brings up the debugging window, this behaves like a breakpoint and stops execution. Now you can see the value of your variables, or you can step through the rest of your code line by line.

My next post will talk about the different kind of console logging functions that are availabole.

There are times you might want to dynamically generate elements on a page and add classes to style them. You can create elements with ids and/or classes with plain Javascript with the following commands:

var newElement = document.createElement(“div”);
//to add a class:
newElement.setAttribute(“class”, “card”);
//to add an id
newElement.setAttribute(“id”, “id_name”);
//to add it to the body:
document.body.appendChild(newElement);
// or to add it to some element:
document.getElementById(“card-box”).appendChild(newElement);

You could also create the string for an element and attach it to the content of a div.

var newElement = '<div id="id_name" class="card">Some Content</div>';
 // This will replace the contents with your new element. 
document.body.innerHTML = newElement;

I was editing the ~/.vagrant.d/boxes//metadata_url file in a box I was testing and when I ran “vagrant box update” I received the following:

$ vagrant box update
==> default: Checking for updates to 'edev-developer'
    default: Latest installed version: 2.0.5
    default: Version constraints: 
    default: Provider: virtualbox
There was an error while downloading the metadata for this box.
The error message is shown below:

Illegal characters found in URL


The illegal character happens to be an EOL char added automatically by your editor, mine was vi, and that is causing the error. I used the following commands to remove the eol character from the file.

perl -pe 'chomp if eof' .vagrant.d/boxes//metadata_url > .vagrant.d/boxes//metadata_url2
mv ~/.vagrant.d/boxes//metadata_url2 ~/.vagrant.d/boxes//metadata_url

Chrome clear DNS cache

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Uncategorized | No comments yet.

I regularly have to test internal systems by adding an entry to the hosts file on my computer. As I keep quite a few tabs open this can cause issues when I need to use the original host system to do some work as I do not want to close all of my tabs to restart Chrome. And when I finish my testing and remove the entry from the hosts file, I need to restart the browser to make sure that I do not have the test DNS entry for my host.

IF you place the following in the address bar:

chrome://net-internals/#dns

You will get a page for DNS that has the following button:
Clear host cache button

This will clear the browser DNS cache and you can use the list below to see the current state of DNS entries below to verify your DNS is as expected.

Install Postgres in Ubuntu

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Linux, OS | No comments yet.

To install PostgreSQL in ubuntu

Add the repo for postgres to the apt repo list:

 sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'
 sudo wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -

To install the postgres database packages:

 sudo apt-get update
 sudo apt-get install postgresql postgresql-contrib

How to exit Vi(m)

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Uncategorized | No comments yet.

So you have found yourself in vi or vim and are unable to exit. The following options are available.

First you need to get to command mode, so either hit or Ctrl-c will switch to command mode from insert mode.
Then use the following:
Exit if there are no changes to the current buffer without writing

:q

Exit even though there are changes to the buffer without writing

:q!

Write and exit ( those are capital Zs)

ZZ

Exit and do not write

ZQ

Sprite Starter Tutorial

digg del.icio.us TRACK TOP
By Eric Downing | Filed in CSS, html, Uncategorized, Web Design | No comments yet.

There are times that you are going to want to add some imagery to your site. This would normally mean using many different image files which will increase the load time for your site. What if you could use a single image and display portions of that image throughout your site.

You could use use a single image called a sprite sheet. This single image file can hold many different images in different tiles for use through out your website. This allows you to load a single file to your page and adjust the viewport in different locations throughtout your site to display the images or icons you are using. Many major websites use this technique.

Let’s use an image like the following:
smiley_sad

The size of this image is 220px x 110px. Each tile of the grid is then a 110px x 110px image. So we need to allow for a viewport to this image that is the size of the tile.

The method we are going to use is a simple div element with the class of “sprite”.

<div class="sprite >
</div>

The CSS for this element will use height and width to define the size of the div. You could also create a placeholder image of the size you want your image to be that is a transparent png and place that inside the div, but that is not necessary.

This snippet is the CSS that we require to show the first image with the smiley face in the div. We specify a height and width for the div to create a viewport into a segment of the image. Next set the background of the div to the image url. The last step is to make sure that the image does not repeat.

.sprite {
width: 110px;
height: 110px;
background: url("http://etechtips.com/wp-content/uploads/2017/04/smiley_sad.png");
background-repeat: no-repeat;
}

In order to display the next image in the sequence you need to use the position properties of the background property. Using the hover pseudo class will allow us to demonstrate the change image update. In the hover property we only need to override the background property with a position offset of -110px in the x direction. Notice that we have used a negative to move the image to the left. The origin of this image is considered 0,0.

.sprite:hover {
  background: url("http://etechtips.com/wp-content/uploads/2017/04/smiley_sad.png") -110px 0;
}

If you used a larger image with more tiles you could scale in both horizontal and vertical directions to show more images.

Example:

If the example doesn’t show in the article, either visit the single page post or checkout this codepen: Smiley Sample

Due to a minor adjustment, I used an offset of -109px for this image or I got a slight shift of the image when I hovered. You should only see the shift of smile to frown when you mouse over the image.

The tiles can be uniformly the same size or you could try packing in many images of different sizes and shapes. Just make sure that you allow for the viewport to not display overlapping images.

Tags: ,

Useful Git Aliases

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Programming, Uncategorized, Utilities | No comments yet.

When you use git often, it is helpful to implement some aliases for frequent commands. Git has config options to allow for shortening commands or allowing you to use a simple command instead of a line of text that is difficult to remember.

Some simple shortcuts:
To make checkout shorter

git config --global alias.co checkout

This will allow you to just type:

git co

To make status shorter:

 
git config --global alias.st status

This will let you see the log with branch indicators and a graph of the branches:

git config --global alias.mylog "log --pretty=format:'%h %s [%an]' --graph"

This is similar to the mylog alias but is useful on a color terminal as it will highlight useful items in the comments:

git config --global alias.lol "log --graph --decorate --pretty=oneline --abbrev-commit ==all"

The results will be the following lines in your .gitconfig file:

[alias]
        st = status
        co = checkout
        mylog = log --pretty=format:'%h %s [%an]' --graph
        lol = log --graph --decorate --pretty=oneline --abbrev-commit --all

Tags:

MySQL: Connect from remote host

digg del.icio.us TRACK TOP
By Eric Downing | Filed in MySQL, Programming | No comments yet.

I have been working on some projects and using my home server to complete some initial testing. I continually have issues connecting from my pc to the database. The remote IP on my VPN changes and I have the server pretty well locked down so need to add the current IP address to the access list.

The database name, user, remotehost ip address, and password are required to use the following command. One note, that you should probably not use the ‘root’ user or admin user when setting this command and insure that you do not leave the access on servers where there could be issues with security.

GRANT ALL PRIVILEGES
ON database.*
TO 'user'@'remotehost'
IDENTIFIED BY 'password';

This command has made it possible to connect using DB Visualizer which makes accessing the database very easy.