Skip to content

Instantly share code, notes, and snippets.

View jswhisperer's full-sized avatar
💭
zzz

Gregory The JSWhisperer jswhisperer

💭
zzz
View GitHub Profile
@jswhisperer
jswhisperer / ajax a form jquery
Created June 23, 2013 14:34
ajax a form jquery
var form = $('#contactForm');
form.submit(function(event) {
$.ajax({
type: form.attr('method'),
url: form.attr('action'),
data: form.serialize(),
success: function (data) {
alert('ok');
}
});
@jswhisperer
jswhisperer / Universal Ajax Load indicator
Created June 26, 2013 19:07
Universal Ajax Load indicator
$(document).ajaxStart(function() {
$("#loading_indicator").show();
}).ajaxStop(function(){
$("#loading_indicator").hide();
});
@jswhisperer
jswhisperer / inline js for show and toggle
Created July 3, 2013 09:45
inline js for show and toggle element by id. Use case, feature phone Opera Mini.
<script>
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
function show(id) {
@jswhisperer
jswhisperer / create a stylesheet
Created July 5, 2013 12:17
Create a stylesheet with JS
var sheet = (function() {
// Create the <style> tag
var style = document.createElement("style");
// Add a media (and/or media query) here if you'd like!
// style.setAttribute("media", "screen")
// style.setAttribute("media", "@media only screen and (max-width : 1024px)")
// WebKit hack :(
style.appendChild(document.createTextNode(""));
@jswhisperer
jswhisperer / email regex
Created July 8, 2013 19:05
Regex for valid email
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
@jswhisperer
jswhisperer / Get, Set, and Delete Cookies
Created July 8, 2013 19:06
Get, Set, and Delete Cookies
//get cookie
function getCookie( name ) {
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ';', len );
if ( end == -1 ) end = document.cookie.length;
@jswhisperer
jswhisperer / the current url
Created July 8, 2013 19:09
the current url
// Retrieve current URL
var url = document.URL;
// Retrieve current root URL
var root = location.protocol + '//' + location.host;
@jswhisperer
jswhisperer / center anything with jquery
Created July 8, 2013 19:09
Center anything with jquery
jQuery.fn.center = function () {
this.css("position","absolute");
this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
return this;
}
//Use the above function as:
$(element).center();
@jswhisperer
jswhisperer / CoffeeScript Jquery better each loop
Created July 19, 2013 07:42
CoffeeScript Jquery better each loop
# Better each() for use with coffeescript
# 1. Wraps child in jquery object
# 2. Sets child first argument, so that fat-binding can be used.
# 3. Sets @ as well, for normal binds
jQuery.fn.loop = (block) ->
for i in @
element = jQuery(i)
res = block.call element, element
break if res == false
@jswhisperer
jswhisperer / REM with Pixel fallback mixin
Created July 22, 2013 07:25
REM with Pixel fallback mixin
@mixin font-size($font-size){
font-size:$font-size +px;
font-size:$font-size / $base-font-size +rem;
}