Skip to content

Instantly share code, notes, and snippets.

@sTiLL-iLL
sTiLL-iLL / XHR.js
Last active February 27, 2023 18:09
XHR and fallback
// simple request with a fallback
function getXHR() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
}
try {
return new ActiveXObject('MSXML2.XMLHTTP.6.0');
}
@sTiLL-iLL
sTiLL-iLL / frag.js
Created August 25, 2014 01:28
Fastest way to add new nodes to the DOM
var frag = document.createDocumentFragment();
ajaxResult.items.forEach(function(item) {
// Create the LI element
var li = document.createElement('li');
li.innerHTML = item.text;
// Do some normal node operations on the LI here,
// like add classes, modify attributes,
// add event listeners, add child nodes, etc.
@sTiLL-iLL
sTiLL-iLL / rclkLclk.js
Created August 29, 2013 21:51
click checker
// figure out which click is left or right
$(element).live('click', function(e) {
if( (!$.browser.msie && e.button == 0) || ($.browser.msie && e.button == 1) ) {
alert("Left Button");
}
else if(e.button == 2)
alert("Right Button");
});
@sTiLL-iLL
sTiLL-iLL / dobVal.js
Last active December 21, 2015 23:39
validate D.O.B.
// Validate date of birth
$("#frm1").submit(function(){
var d = $("#day").val();
var mth = $("#month").val();
var yr = $("#year").val();
// arbitrary number for example
var age = 18;
// see what key was pressed
$(function() {
$(document).keypress(function(e){
switch(e.which){
// "ENTER"
case 13:
alert('enter pressed');
break;
@sTiLL-iLL
sTiLL-iLL / ajaxPreloader.js
Created August 24, 2013 18:53
ajaxn - ified image preloader... its gewd!
(function() {
var f = [];
$.ajax({
url: "imgz.js",
type: "GET",
contentType: "json",
cache: true,
success: function(data) {
var j = JSON.parse(data);
var $c = $("#container");
/*
* Options:
* - before (string): HTML code before the tweet.
* - after (string): HTML code after the tweet.
* - tweets (numeric): number of tweets to display.
*
* Example:
*
* <script type="text/javascript" charset="utf-8">
* $(document).ready(function() {
@sTiLL-iLL
sTiLL-iLL / custom video embed.js
Last active December 21, 2015 10:28
Generate a embed widget on the fly!
//store the URL
var url = "http://vimeo.com/71673549";
//extract the ID
var rgx = /\/\/(www\.)?vimeo.com\/(\d+)($|\/)/;
//the ID: 71673549
var id = url.match(rgx);
var width = '640';
@sTiLL-iLL
sTiLL-iLL / check if cookies are turned on
Last active December 21, 2015 08:38
more helpful JS functions for your pleasure!
// Check if cookies are enabled
$(document).ready(function() {
var dt = new Date();
dt.setSeconds(dt.getSeconds() + 60);
document.cookie = "cookietest=1; expires=" + dt.toGMTString();
var cookiesEnabled = document.cookie.indexOf("cookietest=") != -1;
if(!cookiesEnabled){
//cookies are not enabled
}
@sTiLL-iLL
sTiLL-iLL / functional js type shit
Created August 20, 2013 09:02
helpful snips O' javascript!
// How to get client ip address with jQuery
$.getJSON("http://jsonip.appspot.com?callback=?",function(data){
alert( "Your ip: " + data.ip);
});
// How to parse XML with jQuery
// file.xml: