Skip to content

Instantly share code, notes, and snippets.

@jp26jp
jp26jp / .js
Created March 1, 2017 11:55
Remove all line breaks with javascript
var str = "Hello,\nMy name is, Bob!\n";
str.replace(/(\r\n|\n|\r)/gm,"");
@jp26jp
jp26jp / .htaccess
Created February 10, 2017 18:04
Force .html extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+?)/?$ /$1.html [L,R=301]
@jp26jp
jp26jp / .htaccess
Last active January 18, 2017 13:04
Remove User-Agent from Vary header with .htaccess
Header set Vary "Accept-Encoding"
@jp26jp
jp26jp / .htaccess
Last active January 12, 2017 16:21
Parse php in html with .htaccess
# Place this line of code anywhere in your .htaccess file
AddType application/x-httpd-php .html .htm
# This doesn't work for all servers and so if this doesn't
# work for yours, delete the code above and use the code
# below. If you use the code below, don't forget to remove
# the hash character '#'
# AddHandler application/x-httpd-php .html .htm
@jp26jp
jp26jp / .js
Created November 23, 2016 03:42
Select all text
$('pre code').on('mouseup', function() {
var sel, range;
var el = $(this)[0];
if (window.getSelection && document.createRange) { //Browser compatibility
sel = window.getSelection();
if(sel.toString() == ''){ //no text selection
window.setTimeout(function(){
range = document.createRange(); //range object
range.selectNodeContents(el); //sets Range
sel.removeAllRanges(); //remove all ranges from selection
@jp26jp
jp26jp / .js
Created November 16, 2016 06:40
Use getJSON with a preloader
$(".someSpinnerImage").show();
$.getJSON('file.php', function(json) {
$.each(json, function() {
// Retrieving data from json...
});
$(".someSpinnerImage").hide();
});
@jp26jp
jp26jp / .js
Created November 15, 2016 06:01
Find each ul and increase the data-index value
$('ul').find('ul').each(function(idx){
$(this).attr('data-index', idx);
});
@jp26jp
jp26jp / script.js
Created September 11, 2016 09:36
Programmatically increase color hex brightness
function increase_brightness(hex, percent){
// strip the leading # if it's there
hex = hex.replace(/^\s*#|\s*$/g, '');
// convert 3 char codes --> 6, e.g. `E0F` --> `EE00FF`
if(hex.length == 3){
hex = hex.replace(/(.)/g, '$1$1');
}
var r = parseInt(hex.substr(0, 2), 16),
@jp26jp
jp26jp / throws.swift
Created August 16, 2016 13:59
Check if dictionary and/or dictionary key exists
enum ParserError: ErrorType {
case EmptyDictionary
case InvalidKey
}
struct Parser {
var data: [String : String?]?
func parse() throws {
guard let _ = data else {
@jp26jp
jp26jp / visibility.js
Created August 7, 2016 20:13
Find whether a tab is visible with Page Visibility API
var hidden, visibilityState, visibilityChange;
if (typeof document.hidden !== "undefined") {
hidden = "hidden", visibilityChange = "visibilitychange", visibilityState = "visibilityState";
}
else if (typeof document.mozHidden !== "undefined") {
hidden = "mozHidden", visibilityChange = "mozvisibilitychange", visibilityState = "mozVisibilityState";
}
else if (typeof document.msHidden !== "undefined") {
hidden = "msHidden", visibilityChange = "msvisibilitychange", visibilityState = "msVisibilityState";
}