Skip to content

Instantly share code, notes, and snippets.

View nmoliveira's full-sized avatar

Nuno Oliveira nmoliveira

View GitHub Profile
@nmoliveira
nmoliveira / get-ie-version
Last active August 29, 2015 14:13
Check IE version
// get internet explorer version
// returns the version (i.e 9) or false if is not IE
function getIEVersion() {
return (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) && +(/msie\s(\d+)/i.exec(navigator.userAgent)[1]);
}
// usage example
var ie = getIEVersion();
if (ie && ie < 10) {
alert("This site is best viewed with IE10+");
@nmoliveira
nmoliveira / post-to-url.js
Created June 25, 2014 15:45
Javascript function post to URL
/**
* This function makes a post to a given url with the given parameters
* The form is created on the fly and also the input elements
* @path the URL path
* @params the URL query parameters
*/
function postToUrl(path, params) {
var form = document.createElement("form");
form.setAttribute("method", 'post');
form.setAttribute("action", path);
@nmoliveira
nmoliveira / chain-jquery-deferred.js
Last active August 29, 2015 14:02
Chaining functions with jQuery Deferred
firstTest().then(secondTest).then(thirdTest).then(fourthTest);
function firstTest() {
var def = new $.Deferred();
console.log('First test started');
setTimeout(function () {
console.log('First test finished');
@nmoliveira
nmoliveira / jquery-custom-events-2.js
Last active December 21, 2015 15:19
jQuery Custom Events - one event but more than one handler to be executed.
<!DOCTYPE html>
<html>
<head>
<title>jQuery Custum Events</title>
<script src="jquery.js"></script>
<style type="text/css">
#messages{
width: 200px;
height: 100px;
border: 1px solid #000000
@nmoliveira
nmoliveira / foundation-dropdown.js
Created July 29, 2013 09:16
Dynamic <select> with Zurb Foundation Custom Forms. Requires jQuery.
var selectvalues = { "1": "item 1", "2": "item 2", "3": "item 3"};
var $dropdown = $('#dropdown');
$.each(selectvalues, function(key, value) {
// add to dropdown
$dropdown
.append($('<option>', { value : key })
.text(value));
});
// refresh the elements on zurb dropdown
Foundation.libs.forms.refresh_custom_select($dropdown, true);
@nmoliveira
nmoliveira / checkLeapYear.js
Last active December 20, 2015 06:39
Javascript - check if is a leap year
var isLeap = new Date(year, 1, 29).getMonth() == 1
@nmoliveira
nmoliveira / addOptionsToSelect.js
Created July 26, 2013 10:09
Adding options to an html select using Jquery
// array with values to be added
selectValues = { "1": "test 1", "2": "test 2" };
// for each value in the array append option to select
$.each(selectValues, function(key, value) {
$('#mySelect')
.append($('<option>', { value : key })
.text(value));
});
@nmoliveira
nmoliveira / jquery-custom-events.js
Created July 21, 2013 01:37
jQuery Custom Events
<!DOCTYPE html>
<html>
<head>
<title>jQuery Custum Events</title>
<script src="jquery.js"></script>
<style type="text/css">
#light{
width: 100px;
height: 100px;
background-color: black;
@nmoliveira
nmoliveira / jquery-get-js-script.js
Created June 26, 2013 11:04
Get javascript file with jQuery. Also let that the file be cached.
jQuery.cachedScript = function(url, options) {
// allow user to set any option except for dataType, cache, and url
options = $.extend(options || {}, {
dataType: "script",
cache: true ,
url: url
});
// Use $.ajax() since it is more flexible than $.getScript
@nmoliveira
nmoliveira / backbonejs-collection-handlebarsjs.html
Created June 7, 2013 20:39
Example for rendering a Backbone.js collection using Handlebars.js
<html>
<head>
<title>Backbone Trainning</title>
</head>
<body>
<!-- Template -->
<script id="todos-tmpl" type="text/x-handlebars-template">
{{#each todos}}
<div class='todo-item'>