Category Archives: Web-Dev

CSS loader (loading spinner)

CSS:

.loader {
  width: 50px;
  height: 50px;
  border: 5px solid #f3f3f3;
  border-top: 5px solid #3498db;
  border-radius: 50%;
  animation: spin 1s infinite ease-in-out; /* linear */
  margin: 20px auto;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

HTML:

<div class="loader"></div>

3D loader

CSS:

.loader-3d {
  transform: rotateZ(45deg);
  perspective: 1000px;
  border-radius: 50%;
  width: 120px;
  height: 120px;
  color: #3498db;
  margin: 50px auto;
}
.loader-3d:before,
.loader-3d:after {
  content: '';
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  width: inherit;
  height: inherit;
  border-radius: 50%;
  transform: rotateX(70deg);
  animation: 1s spin linear infinite;
}
.loader-3d:after {
  color: #999;
  transform: rotateY(70deg);
  animation-delay: 0.4s;
}

@keyframes rotate {
  0% {
    transform: translate(-50%, -50%) rotateZ(0deg);
  }
  100% {
    transform: translate(-50%, -50%) rotateZ(360deg);
  }
}

@keyframes rotateccw {
  0% {
    transform: translate(-50%, -50%) rotate(0deg);
  }
  100% {
    transform: translate(-50%, -50%) rotate(-360deg);
  }
}

@keyframes spin {
  0%,
  100% {
    box-shadow: 0.2em 0 0 0 currentcolor;
  }
  12% {
    box-shadow: 0.2em 0.2em 0 0 currentcolor;
  }
  25% {
    box-shadow: 0 0.2em 0 0 currentcolor;
  }
  37% {
    box-shadow: -0.2em 0.2em 0 0 currentcolor;
  }
  50% {
    box-shadow: -0.2em 0 0 0 currentcolor;
  }
  62% {
    box-shadow: -0.2em -0.2em 0 0 currentcolor;
  }
  75% {
    box-shadow: 0 -0.2em 0 0 currentcolor;
  }
  87% {
    box-shadow: 0.2em -0.2em 0 0 currentcolor;
  }
}

HTML:

<div class="loader-3d"></div>

Links:

Windows Tools

Uninstall Windows programs

  1. Click Start, type run and press Enter.
  2. Type appwiz.cpl and click OK.

Show Windows services

  1. Click Start, type run and press Enter.
  2. Type services.msc and click OK.

Show Windows startup and other configs

  1. Click Start, type run and press Enter.
  2. Type msconfig and click OK.

Windows calculator

  1. Click Start, type run and press Enter.
  2. Type calc and click OK.

Other

  1. Click Start, type run and press Enter.
  2. Type %appdata% and click OK.

Find IP-address of the site

Open command line "Start = Run" and 'cmd' and then print - "nslookup microsoft.com" and press "enter"

Check disk

Type in the command line 'chkdsk g: /f' and press "enter". This command will check the disk G and fix the problems (/f param).

Clear DNS cache

Type in the command line "ipconfig /flushdns" and press "enter"

Windows Cleanup

Clean "Temp" folder

  • Click Start, type "%tmp%" and press Enter.
  • Delete all files and folders in "C:\Users\admin\AppData\Local\Temp"

Clean "Download" folder

  • Open "c:\Windows\SoftwareDistribution\Download\" folder
  • Delete all files and folders

Disk Cleanup

  • Open "My Computer", mouse right click on disc "C", "Properties", "Disk Cleanup", "Ok"
  • Open "My Computer", mouse right click on disc "C", "Properties", "Disk Cleanup", "Cleanup System Files"

Logs cleanup

Open 'C:\Windows\Logs\CBS\' folder. You can delete these log files if you are sure your system is running fine.
TrustedInstaller.exe (aka CBS aka Component Based Servicing) will create a new log file during its next run. You may keep only few last ones because it could be useful for troubleshooting issues.

Download and use CCleaner to clean up your Windows registry

Windows Performance

Change Virtual Memory Settings to speed up Windows

Virtual memory for your computer is hard drive space used by Windows when it fills up the physical RAM.
When your system runs low on RAM because an application like Firefox is taking too much memory, Windows moves the least used "pages" of memory out to a hidden file named pagefile.sys in the root of one of your drives to free up more RAM for the applications you are actually using. What this actually means to you is that if you've had an application minimized for a while, and you are heavily using other applications, Windows is going to move some of the memory from the minimized application to the pagefile since it's not being accessed recently. This can often cause restoring that application to take a little longer, and your hard drive may grind for a bit.

If you want to take a look at your own pagefile settings, launch sysdm.cpl from the Start menu search or run box (Win+R) and navigate to Advanced –> Settings –> Advanced –> Change.

By default, Windows uses the boot partition (the partition that contains your operating system files) and it is recommended to set the size of the paging file to 1.5 times the amount of RAM that you have.

To change the virtual memory settings, go to "Start", "Control Panel" and click on "System". Click on the "Advanced tab" and under the "Performance" box, click "Settings". In Windows 7, you’ll need to click on "Advanced System Settings" on the left side to bring up the "System Properties" dialog.

On the "Performance" dialog, click the "Advanced" tab and then click "Change" under the "Virtual Memory" heading.

Now you’ll see the Virtual Memory settings as shown below. In Windows 7 and higher, the "Automatically manage paging file size for all drives" box is checked by default.

To get the best performance it is best to create a paging file on a different partition than the boot partition (the drive that contains that operating system, which is usually the C drive) and to also create it on a separate hard drive. In this way, Windows can handle more I/O requests because the paging file will not have to compete with the system folder that needs to be constantly accessed as Windows runs.

Set the initial and maximum sizes for paging file to the same number to reduce fragmentation of the paging file.

Create paging file on the boot partition and set the initial and maximum sizes to 1.5 times the amount of RAM you have.

 

Windows services which are safe to disable?

  • Print Spooler - if you don’t use a printer or print-to-PDF
  • Remote Registry - it’s not usually running by default, but you can disable it for safety

What happens when you type a URL in browser

Apart from being a very common interview question, this is one of the very first query which lingers around in our mind every time we type a URL in a browser. Here is an attempt to satiate this quest while we delve into the details of what happens in the background when we type a URL in our browsers.

Step 1. URL is typed in the browser.

 

Step 2. If requested object is in browser cache and is fresh, move on to Step  8.

 

Step 3. DNS lookup to find the ip address of the server

when we want to connect to google.com, we actually want to reach out to a server where google web services are hosted. One such server is having an ip address of 74.125.236.65. Now, if you type "http://74.125.236.65" in your browser, this will take you to google home page itself. Which means, "http://google.com" and "http://74.125.236.65" are nothing but same stuff. But, it is not so. Google has multiple servers in multiple locations to cater to the huge volume of requests they receive per second. Thus we should let Google decide which server is best suited to our needs. Using "google.com" does the job for us. When we type "google.com", DNS(Domain Name System) services comes into play and resolves the URL to a proper ip address.

Following is a summary of steps happening while DNS service is at work:

  • Check browser cache: browsers maintain cache of DNS records for some fixed duration. So, this is the first place to resolve DNS queries.
  • Check OS cache: if browser doesn't contain the record in its cache, it makes a system call to underlying Operating System to fetch the record as OS also maintains a cache of recent DNS queries.
  •  Router Cache: if above steps fail to get a DNS record, the search continues to your router which has its own cache.
  •  ISP cache: if everything fails, the search moves on to your ISP. First, it tries in its cache, if not found - ISP's DNS recursive search comes into picture. DNS lookup is again a complex process which finds the appropriate ip address from a list of many options available for websites like Google. You can read more about this here.

For the DNS enthusiasts - here is a great guide worth reading.

Step 4. Browser initiates a TCP connection with the server.

 

Step 5. Browser sends a HTTP request to the server.

Browser sends a GET request to the server according to the specification of HTTP(Hyper Text Transfer Protocol) protocol.

GET http://google.com/ HTTP/1.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:29.0) Gecko/20100101 Firefox/29.0
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Host: google.com
Cookie: datr=1265876274-[...]; locale=en_US; lsd=WW[...]; c_user=2101[...]

Here, browser passes some meta information in the form of headers to the server along with the URL - "http://google.com". User-Agent header specifies the browser properties, Accept-Encoding headers specify the type of responses it will accept. Connection header tells the server to keep open the TCP connection established here. The request also contains Cookies, which are meta information stored at the client end and contain previous browsing session information for the same website in the form of key-value pairs e.g. the login name of the user for Google.

A quick guide to HTTP specification can be found here.

Step 6. Server handles the incoming request

HTTP request made from browsers are handled by a special software running on server - commonly known as web servers e.g. Apache, IIS etc. Web server passes on the request to the proper request handler - a program written to handle web services e.g. PHP, ASP.NET, Ruby, Servlets etc.

For example URL- http://edusagar.com/index.php is handled by a program written in PHP file - index.php. As soon as GET request for index.php is received, Apache(our webserver at edusagar.com) prepares the environment to execute index.php file. Now, this php program will generate a response - in our case a HTML response. This response is then sent back to the browser according to HTTP guidelines.

Step 7. Browser receives the HTTP response

HTTP/1.1 200 OK
Cache-Control: private, no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Pragma: no-cache
Content-Encoding: gzip
Content-Type: text/html; charset=utf-8
Connection: Keep-Alive
Content-length: 1215
Date: Fri, 30 May 2014 08:10:15 GMT

.........<some blob> ................

HTTP response starts with the returned status code from the server. Following is a very brief summary of what a status code denotes:

  •         1xx indicates an informational message only
  •         2xx indicates success of some kind
  •         3xx redirects the client to another URL
  •         4xx indicates an error on the client's part
  •         5xx indicates an error on the server's part

Server sets various other headers to help browser render the proper content. Content-Type tells the type of the content the browser has to show, Content-length tells the number of bytes of the response. Using the Content-Encoding header's value, browsers can decode the blob data present at the end of the response.

Step 8. Browsers displays the html content

Rendering of html content is also done in phases. The browser first renders the bare bone html structure, and then it sends multiple GET requests to fetch other hyper linked stuff e.g. If the html response contains an image in the form of img tags such as <img src="/assets/img/logo.png" />, browser will send a HTTP GET request to the server to fetch the image following the complete set of steps which we have seen till now. But this isn't that bad as it looks. Static files like images, javascript, css files are all cached by the browser so that in future it doesn't have to fetch them again.

Step 9. Client interaction with server

Once a html page is loaded, there are several ways a user can interact with the server. For example, he call fill out a login form to sign in to the website. This also follows all the steps listed above, the only difference is that the HTTP request this time would be a POST instead of GET and along with that request, browser will send the form data to the server for processing (username and password in this case).

Once server authenticates the user, it will send the proper HTML content(may be user's profile) back to the browser and thus user will see that new webpage after his login request is processed.

Step 10. AJAX queries

Another form of client interaction with server is through AJAX(Asynchronous JavaScript And XML) requests. This is an asynchronous GET/POST request to which server can send a response back in a variety of ways - json, xml, html etc. AJAX requests doesn't hinder the current view of the webpage and work in the background. Because of this, one can dynamically modify the content of a webpage by calling an AJAX request and updating the web elements using Javascript.

Hopefully, that gives you an idea of what happens in the background when we do a really simple operation which is type a URL in a browser.

  • Source: http://edusagar.com/articles/view/70/What-happens-when-you-type-a-URL-in-browser

Composition over inheritance

Favor object composition over class inheritance.

The problem with object-oriented languages is they’ve got all this implicit environment that they carry around with them. You wanted a banana but what you got was a gorilla holding the banana and the entire jungle.

CSS Responsive Table

JSFiddle Demo.

HTML:

<table class="table-stacked">
  <thead>
    <tr>
      <th>#</th>
      <th>First Name</th>
      <th>Last Name</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td data-th="#">1</td>
      <td data-th="First Name">Brenda</td>
      <td data-th="Last Name">Bear</td>
    </tr>

    <tr>
      <td data-th="#">2</td>
      <td data-th="First Name">Frank</td>
      <td data-th="Last Name">Fox</td>
    </tr>

    <tr>
      <td data-th="#">3</td>
      <td data-th="First Name">William</td>
      <td data-th="Last Name">Wolf</td>
    </tr>
  </tbody>
</table>

CSS:

.table-stacked {
  width: 100%;
}

.table-stacked thead tr {
  border-bottom: 1px solid gray;
}

@media (max-width: 768px) {
  .table-stacked thead {
    display: none;
  }
  .table-stacked td {
    display: flex;
    border-color: gray;
  }
  .table-stacked td:before {
    content: attr(data-th);
    font-weight: bold;
    width: 30%;
    min-width: 80px;
    padding: 10px;
    padding-left: 0;
    margin: -10px 10px;
    margin-left: 0;
    border-right: 1px solid gray;
    border-width: 2px;
  }
  .table-stacked td:last-child {
    border-bottom: 1px solid gray;
    border-color: gray;
  }
}

Chrome Code Coverage

You can check the CSS and JS code coverage with Chrome Dev Tools:

You can show the Coverage tab from Dev Tools menu by choosing "More tools" => "Coverage".

Alternatively, to open Dev Tools press Cmd+Opt+I (Mac) or Control+Shift+I (Windows, Linux). After this, you can access the Coverage from the Command Menu: Cmd+Shift+P (Mac) or Ctrl+Shift+P (Windows, Linux) and then typing "Coverage".

Progressive enhancement

Progressive Enhancement

Progressive Enhancement

The above figure is often used to demonstrate a minimum viable product, but I think it can also be used to demonstrate a minimum viable experience. The skateboard may be a little slower, but it doesn’t stop the user getting to where they want to go. So, if the user’s browser doesn’t support JavaScript or modern CSS then it doesn’t break, it presents the default experience instead: a button which instructs the user to generate a report. The user will experience a very similar process, but has to perform one extra click.

The beauty of this approach is that the site doesn’t ever appear broken and the user won’t even be aware that they are getting the ‘default’ experience. With progressive enhancement, every user has their own experience of the site, rather than an experience that the designers and developers demand of them.