Forcing a Hyperlink to Download a File in HTML

Modern browsers are smart. When a hyperlink points to a plain text file, a PDF, or a CSS file, the browser previews it instead of downloading it. But sometimes we want users to download the file directly instead of viewing it in the browser. For example, when I previously went to the jQuery official site to download jquery.js, it opened as a preview, and I had to right-click and choose “Save As.” But my goal was clear: I wanted to download the file, not view it! ...

April 26, 2017 · 1 min

CSS Inner Border: Drawing a Border Inside the Box

While designing my personal landing page, I wanted to create an inner border effect, and I learned a clever technique for it. When it comes to a CSS inner border, your first instinct is probably to use padding or margin to make the box bigger. But that doesn’t work: when you enlarge the box, the border moves along with it, so you can’t create an inner border style this way. ...

April 26, 2017 · 1 min

Vue.js Render Object as Table in Beautiful Way

In Vue.js, we can use v-for to render an Array as an HTML table or list nicely. But if we want to render an Object’s keys and values as a table, for example, I use JS Objects and an Array to store a lot of contact info, and each contact has different keys. I want to display the info for Contact 1 in a table. It seems v-for isn’t a good fit here? ...

March 17, 2017 · 1 min

GitHub API Note

Over the past few days, while developing a dashboard, I came into contact with the Trello and GitHub APIs, and I organized some of the information. If you also need to use the GitHub API, you may want to read this post. GitHub also uses the RESTful HTTP API style, so it is very simple to develop with, and it is especially easy to pick up if you have experience integrating HTTP APIs. In my opinion, the GitHub API is one of the most API-friendly I have seen. Not only is the documentation easy to understand, but the API also offers a very complete set of parameters to choose from. There is even a GraphQL API planned for the future, which is really exciting! ...

February 23, 2017 · 5 min

Trello API Notes

Today I was building a dashboard for my company, and I needed a program that calculates the daily development points so the dashboard could show how the points change month by month. So I played around with the Trello API, and here are my quick notes. Some background: our department estimates development progress with Scrum points. Each issue gets a difficulty point value, and we use it together with the Trello Scrum plugin. ...

February 22, 2017 · 3 min

Python 3 CP950 Error When Printing to the Windows Command Prompt

Today, while teaching some juniors how to program, I tried to print a line of Chinese text to the Command Prompt with Python, but I got the error “UnicodeEncodeError: ‘cp950’ codec can’t encode character”. I tried all kinds of methods without success, and in the end I found the answer by searching online. When experienced developers see this error, their first instinct is to assume the string contains a special character, so they immediately add the ignore parameter, but that does not help. When I checked, the character that caused the problem was a Japanese character. As everyone knows, Python 3 uses Unicode strings by default, so it should not have trouble printing anything. It’s not like we’re dealing with the notorious Big5 again… ...

February 21, 2017 · 2 min

Fixing HTML Anchor Content Hidden Behind a Menu

HTML anchors are very handy. When a page is long, an anchor lets visitors jump quickly to a specific section. This technique shows up in many technical documents and blogs. If you’ve seen a URL like https://coder.tw/?p=123#section1, that’s the HTML anchor feature at work. Although it’s convenient, it brings some problems. The most common one: there is a floating menu at the top of the page. When a visitor clicks an anchor link, the browser does scroll to the section, but part of the text ends up hidden behind the menu. I had always been annoyed by this, and today, while reading Facebook’s technical documentation, I found their solution, which is pretty good. ...

February 20, 2017 · 2 min

How to Prevent the Input onFocus Event From Firing After Page onFocus

Recently I was working on adding Google Analytics user event tracking code to a website. I attached the code to inputs using HTML event handlers, so that whenever a user clicks a specific field or button, a record is sent to Google Analytics. This works fine, but I noticed a problem. If a user clicks into an input field, switches to another page (another Chrome tab), and then switches back to my page (Page onFocus), the input’s onFocus event fires automatically! ...

February 16, 2017 · 2 min

Visual Studio Code: Auto-Indenting Pasted Code

Developers who switched from Sublime Text to Visual Studio Code (hereafter VS Code) surely miss the “Paste and Indent” feature! When coding, copying and pasting code is unavoidable, and the indentation often gets messed up afterward, so you have to fix it by hand. Sublime Text provides a so-called Paste and Indent feature that automatically matches pasted code to the indentation of the current file. VS Code has a similar feature, but it works differently and must be enabled in the settings file. ...

February 15, 2017 · 2 min

JavaScript: How to Check Whether a Variable Is NaN

JavaScript has so many strange quirks! Today, while using parseFloat to convert a string into a number, I learned that if the input can’t be converted to a number, it returns NaN (Not a Number). So I wrote the following code to check for it: var score = parseFloat(prompt("Score:")); if(score === NaN) { console.log("Not a Number"); } else { console.log(score); } At first glance, this code seems perfectly reasonable, just like comparing undefined or null directly with equality. Who knew it would completely fail? ...

February 14, 2017 · 2 min