Skip to content

Instantly share code, notes, and snippets.

View jahanson's full-sized avatar

Joe Hanson jahanson

View GitHub Profile
@jahanson
jahanson / pasteimage.html
Created March 3, 2014 17:42
Paste image from clipboard to canvas. Convert to base64 image string.
<!DOCTYPE html>
<html>
<head>
<title>Paste Image</title>
<script type="text/javascript">
var imageObj = new Image();
window.onload = function() {
document.getElementById("pasteTarget").
addEventListener("paste", handlePaste);
var canvas = document.getElementById('canvasTarget');
@jahanson
jahanson / StartOfWeek.cs
Created January 13, 2014 18:52
Get the first day of the week. ex. DateTime dt = DateTime.Now.StartOfWeek(DayOfWeek.Monday); DateTime dt = DateTime.Now.StartOfWeek(DayOfWeek.Sunday); Credit to http://stackoverflow.com/a/38064/1467924
public static class DateTimeExtensions
{
public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
{
int diff = dt.DayOfWeek - startOfWeek;
if (diff < 0)
{
diff += 7;
}
@jahanson
jahanson / animatecss.html
Created December 21, 2013 02:02
Ready to go sample to add class on animation on click and remove when the animation ends via jQuery.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Animate.css test</title>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/animate.css/3.0.0/animate.min.css">
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var myButton = $('#button');
@jahanson
jahanson / asyncHttpGet.cs
Created October 13, 2013 22:44
Asynchronous Http Get with C# Web api.
// GET api/<controller>
async public Task<string> Get()
{
string strResponse = "";
HttpClient client = new HttpClient();
HttpContent responseContent;
HttpResponseMessage response;
response = await client.GetAsync("https://api.domain.com");
@jahanson
jahanson / asyncHttpPost.cs
Created October 13, 2013 22:42
Asynchronous Http Post with data as C# web api.
// GET api/<controller>
async public Task<string> Get()
{
string strResponse = "";
HttpClient client = new HttpClient();
HttpContent responseContent;
HttpResponseMessage response;
var requestContent = new FormUrlEncodedContent(new[] {
new KeyValuePair<string,string>("text","this is a block of text"),
@jahanson
jahanson / serializeToJson.cs
Created October 2, 2013 14:26
Helper function to Serialize almost any object to json.
public static string ToJSON(this object obj)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(obj);
}
public static string ToJSON(this object obj, int recursionDepth)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.RecursionLimit = recursionDepth;
@jahanson
jahanson / RestoreFromBak.sql
Created September 30, 2013 18:39
Restores a database from a .bak file.
USE [master]
ALTER DATABASE [Portal_Sandbox] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
RESTORE DATABASE [Portal_Sandbox]
FROM DISK = N'C:\Portal93013.bak'
WITH FILE = 1,
MOVE N'Portal' TO N'D:\Data Files\Portal_sandbox.mdf',
MOVE N'Portal_log' TO N'D:\Data Files\Portal_sandbox_log.ldf',
NOUNLOAD, REPLACE, STATS = 5
@jahanson
jahanson / watchAngularfireVariable.js
Created September 14, 2013 20:25
Snippet to watch for when a variable is populated by angularfire.
$scope.$watch('variable', function() {
if($scope.variable.length > 0) {
// greater than zero, now we can enable input fields and such.
}
});
@jahanson
jahanson / updateRepoGitignore.sh
Last active December 23, 2015 01:59
Linux git command that removes files in your repo that apply to the .gitignore but doesn't delete them. Ex. You added some rules after a while and need them to be applied immediately.
#!/bin/bash
(GIT_INDEX_FILE=some-non-existent-file \
git ls-files --exclude-standard --others --directory --ignored -z) |
xargs -0 git rm --cached -r --ignore-unmatch --
@jahanson
jahanson / IEvsOthersAjax.js
Created August 7, 2013 20:34
Uses XDR in IE and $.ajax in any other browser. Make sure to include JQuery & Modernizr for browser detection.
$(document).ready(function() {
var BrowserDetect =
{
init: function ()
{
this.browser = this.searchString(this.dataBrowser) || "Other";
this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "Unknown";
},
searchString: function (data)