Skip to content

Instantly share code, notes, and snippets.

  • Using download attribute for <a>
<a href="path_to_file" download="proposed_file_name">Download</a>

Pros: fast, don't need program from server (the link doesn't need have Content-Disposition: attachment is set from server)

Cons: Partial support by browsers. check http://caniuse.com/#feat=download.

  • <button>
@khoand0000
khoand0000 / shared.php
Created November 29, 2015 12:22
My shared class #array
<?php
/**
* Created by PhpStorm.
* User: khoand
* Date: 11/29/15
* Time: 7:14 PM
*/
class Shared
{
@khoand0000
khoand0000 / pagination-guide.md
Last active December 6, 2015 20:28
Pagination guide #convention #rules Reference: http://www.popshops.com/support/api-3-products
  • Required request parameters: page (start 1, not 0)
  • Optional request parameters: results_per_page
  • RESTful pattern, don't include required parameters in url: http://myapi.com/items (not http://myapi.com/items/:page), include required and optional parameters in query parameters: http://api.com/items?page=1&results_per_page=50. Because there is just GET request is used for pagination, so always using query parameters (don't include them in json body, just using json body when request is PUT, POST)
  • Response: json object contains at least: count (total items), items (array)
{
  count: 10,
  items: [
    {name: '', ...},
 {name: '', ...}
@khoand0000
khoand0000 / angular-foreach.js
Last active November 26, 2015 19:34
Using angular.forEach to loop array that is returned $promise. Because result contains more information of angularjs: $promise, $resolved
// data is array
vm.data = Data.getData(function () {
angular.forEach(vm.data, function(item) {
console.log(item);
});
// don't use
// for (var i = 0; i < vm.data.length; i++)
// { }
@khoand0000
khoand0000 / using-overflow-hidden-carefully.md
Last active December 15, 2015 16:03
using overflow:hidden carefully with sub-menu is inside the element

My situation: I want my menu contains 2 items: username text, user icon (contain sub-menu with 1 item: Sign Out) always same align horizontal even resize window (becaues window's width is not enough to contain both items. So I used the css code like

ul.navbar-top-links {
  overflow: hidden;
  height: 50px; // example
}

with html code

@khoand0000
khoand0000 / change-properties-jquery.md
Created November 24, 2015 18:31
Change properties of elements (ex: change height, width, color, ...) by jquery

My situation: I want to update height of side-menu (it is dynamic, its items are loaded from server). I know need to using $timeout to waiting angular build DOM done. Bellow code is right

  $scope.data = WebService.getData(function () {
      // doing something with $scope.data
      
      // must put the in here, after server return result.
      // angular using the result to build DOM again (changed everything will be destroyed)
      // using $timeout to waiting angular done
 $timeout(function() {
@khoand0000
khoand0000 / remove-Vietnamese-phone.php
Created November 22, 2015 07:35
remove phone (Vietnam) from content
$content = preg_replace('#[\d]{3,4}[\.\- \s]?[\d]{2,3}[\.\- \s]?[\d]{2,3}[\.\- \s]?[\d]{1,2}#mius', '...', $content);
// 0999990999
// 099 999 0999
// 099 999 09 99
// 099 99 90 99
// 0999 99 0999
// 0999 990 999
// 0999 99 09 99
// 09999999990
// 0999 999 9990
@khoand0000
khoand0000 / phone-validator.md
Created November 22, 2015 06:53
validate phone input
@khoand0000
khoand0000 / padding-margin.md
Created November 22, 2015 05:11
padding vs. margin
  • Padding relates to click, hover on the element; so just using padding when showing box of the element (bound by border) is larger
  • Using margin when increase space between 2 elements
@khoand0000
khoand0000 / centering-css-complete-guide.md
Last active February 10, 2016 04:35
middle vertical align. Just using the link and apply suitable case: https://css-tricks.com/centering-css-complete-guide/

Take note when using https://css-tricks.com/centering-css-complete-guide/

  • Vertically
    • Is it inline or inline-* elements (like text or links)?
      • Is it a single line?
        • don't know height of element: padding-top == padding-bottom
        • know height of element: using line-height
            .center-text-trick {

height: 100px; /* apply inline-block element, inline element doesn't need it */