Skip to content

Instantly share code, notes, and snippets.

@JeffJacobson
JeffJacobson / google-analytics-honor-doNotTrack.js
Last active October 26, 2018 21:11
Google Analytics setup respecting navigator.doNotTrack.
// Setup Google Analytics, but not if user has specified that they don't want to be tracked.
(function (dnt, cookieDomain) {
var scriptTag, hostRe = new RegExp(cookieDomain.replace(".", "\\.") + "$");
if (dnt !== "yes" && dnt !== "1") {
window.ga = window.ga || function () { (ga.q = ga.q || []).push(arguments) }; ga.l = +new Date;
ga('create', 'YOUR-ID-HERE', hostRe.test(location.host) ? cookieDomain : "auto");
ga(function (tracker) {
tracker.set("appName", "Your app name here");
tracker.send('pageview');
});
@JeffJacobson
JeffJacobson / JavaScript Date value to .NET DateTimeOffset.cs
Last active February 13, 2017 18:24
Convert a JavaScript date expressed in milliseconds into a .NET DateTimeOffset.
/// <summary>
/// Converts from a JavaScript Date value in milliseconds to a <see cref="DateTimeOffset"/>.
/// </summary>
/// <param name="milliseconds">Number of milliseconds since 1970-1-1T00:00:00</param>
/// <returns>Returns the <see cref="DateTimeOffset"/> equivalent of <paramref name="milliseconds"/>.</returns>
public static DateTimeOffset FromJSDateToDateTimeOffset(this double milliseconds)
{
return new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero).AddMilliseconds(milliseconds * 1000);
}
@JeffJacobson
JeffJacobson / project-coordinate-arrays.js
Created January 30, 2014 20:51
Project arrays of coordinates using Proj4JS
// For use withhttp://proj4js.org/
/** @typedef {(string|proj4.Proj)} Projection
*
*/
/** @typedef {object} ThisProjectionInfo
* @property {?Projection} inPrj
* @property {?Projection} outPrj
*/
@JeffJacobson
JeffJacobson / get-query-string-parameter.js
Created August 8, 2013 21:57
Gets a query string parameter.
/** Gets a query string parameter.
@returns {String|null} Returns the value of the query string parameter, or null if that parameter is not defined.
*/
function getQueryStringParameter(/** {String} */ key) {
var keyRe, match, output = null;
if (document.location.search.length) {
keyRe = new RegExp(key + "=([^\\&]+)", "i");
match = document.location.search.match(keyRe);
if (match) {
output = match[1];
@JeffJacobson
JeffJacobson / CsvToDictionaryList.cs
Last active December 16, 2015 07:38
Converts a Text Table file to Dictionaries.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Dict = System.Collections.Generic.Dictionary<string, object>;
/*
Licensed under the MIT license (http://opensource.org/licenses/MIT)
Copyright (c) 2013 Washington State Department of Transportation
@JeffJacobson
JeffJacobson / MapQuestInArcGisJSApi.html
Last active September 28, 2019 09:54
Demonstrates using MapQuest Open map service tiles with ArcGIS API for JavaScript.
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.3/js/esri/css/esri.css" />
<script src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=3.3compact" type="text/javascript"></script>
<style>
html, body, #map, map.container {
height: 100%;
@JeffJacobson
JeffJacobson / WsdotTravelerApiProxy.ashx.cs
Last active December 10, 2015 06:08
Demonstrates how to detect a WSDOT Traveler API REST Request URL and append an access code from web.config. Designed for use with a Proxy page, as described at http://help.arcgis.com/en/webapi/javascript/arcgis/help/jshelp_start.htm#jshelp/ags_proxy.htm.
/// <summary>
/// Appends an AccessCode parameter to the input URI if it is a WSDOT Traveler Info REST API URI and does not already have an AccessCode parameter.
/// </summary>
/// <param name="uri">The URI passed to the proxy. Note that this string will be modified if an access code is determined to be necessary.</param>
/// <example>
/// <code><![CDATA[public void ProcessRequest (HttpContext context) {
/// HttpResponse response = context.Response;
/// // Get the URL requested by the client (take the entire querystring at once to handle the case of the URL itself containing querystring parameters)
/// string uri = context.Request.Url.Query.Substring(1);
/// AddTravelerApiAccessCodeIfNecessary(ref uri);
@JeffJacobson
JeffJacobson / Usgs.Elevation.cs
Created October 23, 2012 18:55
Consuming USGS Elevation Web Service
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.269
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
@JeffJacobson
JeffJacobson / WebMercatorGeographicConversion.cs
Created October 23, 2012 18:12
Functions to convert between web mercator and geographic coordinates.
// from http://www.gal-systems.com/2011/07/convert-coordinates-between-web.html
private void ToGeographic(ref double mercatorX_lon, ref double mercatorY_lat)
{
if (Math.Abs(mercatorX_lon) < 180 && Math.Abs(mercatorY_lat) < 90)
return;
if ((Math.Abs(mercatorX_lon) > 20037508.3427892) || (Math.Abs(mercatorY_lat) > 20037508.3427892))
return;
@JeffJacobson
JeffJacobson / splitCamelCase.js
Last active August 26, 2023 10:42
Splits camelCase or PascalCase words into individual words.
/**
* Splits a Pascal-Case word into individual words separated by spaces.
* @param {Object} word
* @returns {String}
*/
function splitPascalCase(word) {
var wordRe = /($[a-z])|[A-Z][^A-Z]+/g;
return word.match(wordRe).join(" ");
}