Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Last active March 1, 2022 19:46
Show Gist options
  • Save kristopherjohnson/4046616 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/4046616 to your computer and use it in GitHub Desktop.
Random Javascript snippets/examples
// Get a reference to the global object (`window` in the browser, `global` on the server).
var root = (function () {
return this || (0 || eval)('this');
}());
var elemWithId = document.getElementById('anId');
elemWithId.innerHTML = markup;
var elemsWithName = document.getElementsByName('aName');
var firstElem = elems[0];
// when Enter key is pressed in a text field, click an associated button
document.getElementById('textBox').onkeypress = function (e) {
var evt = e ? e : window.event;
if (evt.keyCode == 13) {
document.getElementById('enterButton').click();
return false;
}
};
if (window.console && window.console.log) {
window.console.log("This is a debugging message.");
}
var concatenatedString = "Hello, " + "world!";
concatenatedString += " And hello again!";
var add = function(a, b) { return a + b; };
var handleVariableArgumentList = function() {
var i;
for (i = 0; i < arguments.length; ++i) {
// do something with arguments[i]
}
}
var person = { first_name: 'Kris', last_name: 'Johnson' };
person.middle_initial = 'D';
person["age"] = 29;
delete person.middle_initial; // person.middle_initial now undefined
function Person(first, last) {
this.first = first;
this.last = last;
}
Person.prototype.fullName = function() {
return this.first + ' ' + this.last;
}
Person.prototype.fullNameReversed = function() {
return this.last + ', ' + this.first;
}
var p = new Person("Kris", "Johnson");
var fullName = p.fullName();
var data = [1, 2, 3, 4, 5];
var i;
for (i = 0; i < data.length; ++i) {
// do something with data[i]
}
data[5] = 6;
data[1000] = 3.14; // data.length is now 1001; data[6] is undefined
for (prop in obj) {
if (obj.hasOwnProperty(prop)) {
// prop is a member of the object, not its prototype
}
}
for (prop in obj) {
if (typeof prop !== 'function') {
// prop is not a function
}
}
try {
// Thrown object should have properties "name" and "message"
throw new Error("An error occurred");
}
catch (e) {
// Do something with e.name and e.message
}
finally {
// This will always run
}
// jQuery
// <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
$(document).ready(function() {
// do stuff when DOM is ready
});
// Microsoft ASP.NET AJAX
var label = $get("<%= Label1.ClientID %>"); // get DOM element
var control = $find("<%= MyControl1.ClientID %>"); // get client control
// serialize objects to/from JSON using JavaScriptSerializer (requires ScriptManager)
var contact = Sys.Serialization.JavaScriptSerializer.deserialize(contactString);
var contactString = Sys.Serialization.JavaScriptSerializer.serialize(contact);
// AngularJS
// <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
angular.module('myApp', [])
.value('myConstant', 123)
.controller('myController', ['$scope', function($scope) {
// Set properties of $scope
}])
.filter('myCustomFilter', function() {
return function(input) {
return '<<<' + input + '>>>';
};
})
.factory('myService', function() {
return {
doSomething: function() { /* ... */ };
};
});
@kristopherjohnson
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment