<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>JavaScript on Lin's Blog</title><link>https://coder.tw/en/tags/javascript/</link><description>Recent content in JavaScript on Lin's Blog</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Fri, 17 Mar 2017 01:39:34 +0000</lastBuildDate><atom:link href="https://coder.tw/en/tags/javascript/index.xml" rel="self" type="application/rss+xml"/><item><title>Vue.js Render Object as Table in Beautiful Way</title><link>https://coder.tw/en/posts/vue-js-render-object-as-table-in-beautiful-way/</link><pubDate>Fri, 17 Mar 2017 01:39:34 +0000</pubDate><guid>https://coder.tw/en/posts/vue-js-render-object-as-table-in-beautiful-way/</guid><description>&lt;p&gt;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&amp;rsquo;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&amp;rsquo;t a good fit here?&lt;/p&gt;</description></item><item><title>How to Prevent the Input onFocus Event From Firing After Page onFocus</title><link>https://coder.tw/en/posts/how-to-prevent-js-input-onfocus-event-fire-after-page-onfocus/</link><pubDate>Thu, 16 Feb 2017 11:27:59 +0000</pubDate><guid>https://coder.tw/en/posts/how-to-prevent-js-input-onfocus-event-fire-after-page-onfocus/</guid><description>&lt;p&gt;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&amp;rsquo;s onFocus event fires automatically!&lt;/p&gt;</description></item><item><title>JavaScript: How to Check Whether a Variable Is NaN</title><link>https://coder.tw/en/posts/javascript-check-variable-nan/</link><pubDate>Tue, 14 Feb 2017 22:44:05 +0000</pubDate><guid>https://coder.tw/en/posts/javascript-check-variable-nan/</guid><description>&lt;p&gt;JavaScript has so many strange quirks! Today, while using parseFloat to convert a string into a number, I learned that if the input can&amp;rsquo;t be converted to a number, it returns NaN (Not a Number). So I wrote the following code to check for it:&lt;/p&gt;
&lt;pre&gt;var score = parseFloat(prompt("Score:"));
if(score === NaN) {
console.log("Not a Number");
} else {
console.log(score);
}&lt;/pre&gt;
&lt;p&gt;At first glance, this code seems perfectly reasonable, just like comparing undefined or null directly with equality. Who knew it would completely fail?&lt;/p&gt;</description></item><item><title>Worried About Visitors Copying Your Articles? Use JS to Automatically Append a Reminder or Disable Copying!</title><link>https://coder.tw/en/posts/js-auto-append-reminder-on-copy/</link><pubDate>Sun, 12 Feb 2017 23:43:41 +0000</pubDate><guid>https://coder.tw/en/posts/js-auto-append-reminder-on-copy/</guid><description>&lt;p&gt;Many content sites have a mechanism that automatically appends the original article URL when text is copied, such as Mojing Lyrics and Zhihu. Some sites even ban users from copying text altogether. Achieving these features isn&amp;rsquo;t difficult; it only takes a few lines of simple JavaScript. But first you should think about whether your site really needs such a feature. When a regular visitor copies text and pastes it into Facebook, does that really harm your site? And if you truly want to stop visitors from copying text, these tricks only keep out the honest, not the determined. Are you really protecting your interests, or just making your site harder to use?&lt;/p&gt;</description></item><item><title>Handling File Uploads on the Frontend With JS: Preview, Size, Dimensions, and Format</title><link>https://coder.tw/en/posts/js-file-upload-preview-validation/</link><pubDate>Thu, 09 Feb 2017 13:44:33 +0000</pubDate><guid>https://coder.tw/en/posts/js-file-upload-preview-validation/</guid><description>&lt;p&gt;What I hate most when uploading a file is being told five minutes later that the file is too big or the dimensions are wrong! We usually leave upload validation to the backend, so a file has to be uploaded before we can check whether it meets the requirements. Images in particular also need their aspect ratio checked. If all of this is left entirely to the backend, the user experience can suffer. When I was researching profile picture uploads at NewTalk, I found that all of this can actually be handled on the frontend. With JS and HTML5 APIs, you can check the file&amp;rsquo;s format, size (20MB), and dimensions (100x100). You can even do previews and client-side image cropping and conversion!&lt;/p&gt;</description></item><item><title>Measuring Function Execution Time in JavaScript</title><link>https://coder.tw/en/posts/javascript-measure-function-time/</link><pubDate>Tue, 24 Jan 2017 18:56:38 +0000</pubDate><guid>https://coder.tw/en/posts/javascript-measure-function-time/</guid><description>&lt;p&gt;To measure a function&amp;rsquo;s execution time in JavaScript, you can subtract the timestamp taken before the function runs from the timestamp taken after it finishes. Most people use new Date().getTime() to get a timestamp, but if your function runs in less than 1 millisecond, this won&amp;rsquo;t work, because the smallest unit of a timestamp is the millisecond. So I recommend the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Performance/now"&gt;Window.Performance API&lt;/a&gt;. It gives you the number of milliseconds since the page started loading, with precision down to a thousandth of a millisecond.&lt;/p&gt;</description></item><item><title>JavaScript String Replace All</title><link>https://coder.tw/en/posts/javascript-string-replace-all/</link><pubDate>Mon, 16 Jan 2017 01:20:29 +0000</pubDate><guid>https://coder.tw/en/posts/javascript-string-replace-all/</guid><description>&lt;p&gt;In JavaScript, String.replace may behave differently than you expect. When you want to replace a string, you&amp;rsquo;ll find that replace only replaces the first occurrence. For example, &amp;ldquo;AAA&amp;rdquo;.replace(&amp;ldquo;A&amp;rdquo;, &amp;ldquo;B&amp;rdquo;) gives &amp;ldquo;BAA&amp;rdquo;, not the &amp;ldquo;BBB&amp;rdquo; you might expect. This is because the first argument of JS replace is actually a regular expression, not a string. If you pass in a string, only the first match is replaced, which leads to results you didn&amp;rsquo;t expect.&lt;/p&gt;</description></item><item><title>JavaScript: Manipulating Browser History, Changing URLs, and the Back Button Event (Essential for SPAs)</title><link>https://coder.tw/en/posts/javascript-manipulate-browser-history/</link><pubDate>Sat, 14 Jan 2017 12:59:02 +0000</pubDate><guid>https://coder.tw/en/posts/javascript-manipulate-browser-history/</guid><description>&lt;p&gt;Nowadays JavaScript is everywhere. Using JS to manipulate web pages, for things like Ajax, page switching, and pop-up windows, is more and more common. There are even JS frameworks such as React JS and Vue JS that help you develop Single Page Applications (SPAs). When you manipulate a web page with JS, the thing you fear most is URL changes and history. For example, if you switch pages with jQuery, the URL doesn&amp;rsquo;t change, because it&amp;rsquo;s a JS operation. And when the user clicks the browser&amp;rsquo;s Back button in the top left corner, they go straight to another page, because JS operations aren&amp;rsquo;t recorded in the history (in fact, they can&amp;rsquo;t be). To solve this series of problems, you can use history.pushState, history.replaceState, and the onpopstate event provided by HTML5.&lt;/p&gt;</description></item><item><title>Implementing a Round-Up-to-Whole-Number Feature: Ideas and Methods</title><link>https://coder.tw/en/posts/rounding-integer-approach/</link><pubDate>Wed, 11 Jan 2017 23:30:33 +0000</pubDate><guid>https://coder.tw/en/posts/rounding-integer-approach/</guid><description>&lt;p&gt;Recently I came across a &amp;ldquo;round up&amp;rdquo; feature on Zeczec. For example, if a checkout total is NT$1359, pressing the round-up button changes it to NT$1400. It&amp;rsquo;s a fun feature, but when I actually tried to implement it, I found that rounding up isn&amp;rsquo;t easy. There are many non-technical concerns. Here I&amp;rsquo;ll record the problems I ran into while implementing it, the process I went through, and the final solution!&lt;/p&gt;</description></item><item><title>JavaScript Page Load, Focus, UnFocus, and Exit Events</title><link>https://coder.tw/en/posts/javascript-page-load-focus-exit-events/</link><pubDate>Tue, 03 Jan 2017 20:43:11 +0000</pubDate><guid>https://coder.tw/en/posts/javascript-page-load-focus-exit-events/</guid><description>&lt;p&gt;Most people know how to use JavaScript to record when a user enters a web page, but have you ever thought about how to tell when a user has &amp;ldquo;closed&amp;rdquo; it? Browsers today show multiple web pages as tabs, so it&amp;rsquo;s possible the user is actually looking at someone else&amp;rsquo;s page while yours just sits there. For example, YouTube often runs into this: a user leaves the page open to listen to music, but in fact they&amp;rsquo;re looking at Facebook&amp;hellip;&lt;/p&gt;
&lt;p&gt;Today I&amp;rsquo;ll briefly note the JS page load and close events, as well as the focus (entering the page) and unfocus (leaving the page) events. With these events, you can record user behavior for analysis, and even do things like stop playing music when the user leaves the page.&lt;/p&gt;</description></item><item><title>Copying Text to the Clipboard with JavaScript</title><link>https://coder.tw/en/posts/javascript-copy-to-clipboard/</link><pubDate>Sat, 10 Dec 2016 15:19:09 +0000</pubDate><guid>https://coder.tw/en/posts/javascript-copy-to-clipboard/</guid><description>&lt;p&gt;In the past, using JavaScript to copy and paste text through the clipboard was very cumbersome. While developing NTUST.ME, a short URL service for National Taiwan University of Science and Technology, I wanted to make it easy for people to copy the shortened URLs, so I looked into the clipboard API in JavaScript. I found that newer browsers have an execCommand function that supports many handy text operations, such as copy and select all. Let me introduce it to you!&lt;/p&gt;</description></item><item><title>Enable Cross-Origin Resource Sharing (CORS) for Phaser.js</title><link>https://coder.tw/en/posts/enable-cross-origin-resource-sharing-cors-for-phaser-js/</link><pubDate>Fri, 02 Dec 2016 14:01:31 +0000</pubDate><guid>https://coder.tw/en/posts/enable-cross-origin-resource-sharing-cors-for-phaser-js/</guid><description>&lt;p&gt;When you load resources and assets from a different domain in Phaser JS, the browser&amp;rsquo;s same-origin policy blocks them, and you get a pile of error messages, such as:&lt;/p&gt;
&lt;blockquote&gt;XMLHttpRequest cannot load http://s.ntustcoding.club/scroll-game-workshop/map.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://s.codepen.io' is therefore not allowed access.&lt;/blockquote&gt;
Actually, Phaser itself supports Cross-Origin Resource Sharing (CORS). You can solve this with one simple line, as long as the web hosting you use also supports CORS.</description></item><item><title>【jQuery】Lazyload - Delayed Image Loading</title><link>https://coder.tw/en/posts/jquery-lazyload/</link><pubDate>Wed, 27 Apr 2011 19:15:30 +0000</pubDate><guid>https://coder.tw/en/posts/jquery-lazyload/</guid><description>&lt;p&gt;&lt;a href="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/04/2011-04-27_191427.jpg"&gt;&lt;img class="size-large wp-image-429 aligncenter" title="2011-04-27_191427" src="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/04/2011-04-27_191427-540x481.jpg" alt="" width="540" height="481" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Photography websites often have a lot of images. When visitors browse these sites, it takes quite a while to load because so many photos have to be loaded. This has a big impact on the website&amp;rsquo;s host traffic, and it also increases the CPU time on the visitor&amp;rsquo;s side a lot. Here I want to introduce Lazyload, a jQuery plugin that delays image loading. In other words, when you enter a website, it doesn&amp;rsquo;t load all the images at once. It only loads some of them, and as you scroll the browser, it loads &amp;ldquo;the images in the section you are looking at,&amp;rdquo; so the page loads faster.&lt;/p&gt;</description></item><item><title>[JavaScript] encodeURIComponent: A Function for Encoding URLs</title><link>https://coder.tw/en/posts/javascript-encodeuricomponent/</link><pubDate>Wed, 22 Jul 2009 14:49:56 +0000</pubDate><guid>https://coder.tw/en/posts/javascript-encodeuricomponent/</guid><description>&lt;p&gt;When you use JavaScript to make GET requests, you&amp;rsquo;ll often run into the fact that JavaScript requires the URL to be encoded. For functions that need an encoded URL, you can use encodeURIComponent to encode it.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2009/07/2009-07-22_144915.jpg"&gt;&lt;img class="alignnone size-full wp-image-1666" title="2009-07-22_144915" src="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2009/07/2009-07-22_144915.jpg" alt="2009-07-22_144915" width="397" height="134" /&gt;&lt;/a&gt;&lt;/p&gt;</description></item></channel></rss>