Skip to content

Instantly share code, notes, and snippets.

View prettycode's full-sized avatar

Chris prettycode

  • Seattle, WA
View GitHub Profile
@prettycode
prettycode / submitForm.js
Last active December 16, 2015 07:49
Use this function to submit an HTML form without actually having a form on the page. Instead of an existing form, pass the function a *flat* JavaScript object with the form values to submit. This is a synchronous operation; the browser loads the response as its new window location. Requests with "Content-Type: application/x-www-form-urlencoded".
var submitForm = function(config) {
if (!config) {
throw new Error();
}
else {
config.method = config.method || 'post';
config.data = config.data || {};
config.url = config.url || (function() {
throw new Error('Missing required `url` argument');
@prettycode
prettycode / js2markup.htm
Last active December 16, 2015 06:49
Generate HTML from JavaScript object literals.
<!DOCTYPE html>
<html>
<head>
<title>Proof of Concept</title>
<script>
// TODO how should Arrays be handled?
// TODO strict mode (no '\n' or padding) to allow for significant whitespace (e.g. for <pre>, <textarea>)
var toHTML = function(value, name, indent) {
var pad = new Array((indent = indent || 0)).join(' ');
@prettycode
prettycode / js2xml.htm
Created April 16, 2013 05:16
JavaScript/JSON -> XHTML serializer.
<!DOCTYPE html>
<html xmlns:pretty="http://prettycode.org/xmlns/json2xml">
<head>
<title>Proof of Concept</title>
<script>
var XML = {
defaultNamespace: undefined,
stringify: function(ns, value, name) {
if (typeof ns === 'undefined') {
ns = this.defaultNamespace;
// Working example: http://www.jsfiddle.net/mUrVB/22/
(function($) {
$.fn.selection = function() {
var start = this[0].selectionStart,
end = this[0].selectionEnd;
if (start === end) {
return null;