Skip to content

Instantly share code, notes, and snippets.

View adamculpepper's full-sized avatar

Adam Culpepper adamculpepper

View GitHub Profile
@adamculpepper
adamculpepper / center.target.js
Last active August 29, 2015 14:07
Center an element to another element
// demo: http://jsfiddle.net/adamculpepper/9bf2r9uj
centerElement($(".center-it.me"), $(".center-it.to"));
function centerElement(me, to) {
var toWidth = to.outerWidth();
var meWidth = me.outerWidth();
var meCenter = Math.abs(meWidth / 2) - (toWidth / 2);
me.css({
@adamculpepper
adamculpepper / break-iframes.js
Created October 20, 2014 13:36
Break out of iframes
// Break out of iframes
if (window != window.top) {
top.location.href = location.href
}
@adamculpepper
adamculpepper / current-year.js
Created October 20, 2014 17:52
Current Year
var year = new Date().getFullYear();
@adamculpepper
adamculpepper / url-body-classes.js
Created October 20, 2014 21:37
Split Up URL slugs, add as body classes
var loc = window.location;
var url = (location.href);
loc.origin || (loc.origin = loc.protocol + '//' + loc.host);
url = url.split('?')[0];
var slugs = $.trim((url.replace(loc.origin, '')).split("/").join(" "));
if (slugs == "") {
slugs = "home";
}
@adamculpepper
adamculpepper / business-catalyst-secure-redirect.js
Created October 20, 2014 21:42
Redirect if protocol is secure, but not coming from the store (Business Catalyst, requires jQuery)
// Coming from secure protocol and user is NOT inside the store, redirect to a non-secure connection
if (location.protocol == "https:") {
if (url.search("worldsecuresystems.com") < 0) {
location.protocol = "http:";
}
}
@adamculpepper
adamculpepper / search-page-body-class.js
Created October 20, 2014 21:44
Body class added on Search results page (Business Catalyst, requires jQuery)
if (url.search("Default.aspx?A=Search")) {
$("body").addClass("search");
}
@adamculpepper
adamculpepper / ajax-form-submit.js
Last active August 29, 2015 14:08
AJAX form submission (using FormKeep & requires jQuery)
//Simple form
$("form").submit(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'https://formkeep.com/f/7212a0cb4563',
data: $(this).serialize(),
success: function() {
//window.location = "http://google.com";
@adamculpepper
adamculpepper / script-processing-time.php
Created November 10, 2014 14:52
Calculate the page/script processing time
<?
$start = (float) array_sum(explode(' ',microtime()));
?>
<!-- CONTENT HERE -->
<?
$end = (float) array_sum(explode(' ',microtime()));
echo "Processing time: ". sprintf("%.4f", ($end-$start))." seconds";
?>
@adamculpepper
adamculpepper / 0_reuse_code.js
Last active August 29, 2015 14:12
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@adamculpepper
adamculpepper / javascript_resources.md
Last active August 29, 2015 14:12 — forked from jookyboi/javascript_resources.md
Here are a set of libraries, plugins and guides which may be useful to your Javascript coding.

Libraries

  • jQuery - The de-facto library for the modern age. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers.
  • Backbone - Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.
  • AngularJS - Conventions based MVC framework for HTML5 apps.
  • Underscore - Underscore is a utility-belt library for JavaScript that provides a lot of the functional programming support that you would expect in Prototype.js (or Ruby), but without extending any of the built-in JavaScript objects.
  • lawnchair - Key/value store adapter for indexdb, localStorage