Skip to content

Instantly share code, notes, and snippets.

Avatar

Joe Hanson jahanson

  • San Antonio, TX
View GitHub Profile
@jahanson
jahanson / pasteimage.html
Created Mar 3, 2014
Paste image from clipboard to canvas. Convert to base64 image string.
View pasteimage.html
<!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 Jan 13, 2014
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
View StartOfWeek.cs
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 Dec 21, 2013
Ready to go sample to add class on animation on click and remove when the animation ends via jQuery.
View animatecss.html
<!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 Oct 13, 2013
Asynchronous Http Get with C# Web api.
View asyncHttpGet.cs
// 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 Oct 13, 2013
Asynchronous Http Post with data as C# web api.
View asyncHttpPost.cs
// 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 Oct 2, 2013
Helper function to Serialize almost any object to json.
View serializeToJson.cs
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 Sep 30, 2013
Restores a database from a .bak file.
View RestoreFromBak.sql
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 Sep 14, 2013
Snippet to watch for when a variable is populated by angularfire.
View watchAngularfireVariable.js
$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 Dec 23, 2015
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.
View updateRepoGitignore.sh
#!/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 Aug 7, 2013
Uses XDR in IE and $.ajax in any other browser. Make sure to include JQuery & Modernizr for browser detection.
View IEvsOthersAjax.js
$(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)