Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Thursday, May 17, 2012

Javascript console.log() and IE

Arg how I loath IE, why doesn't it support console.log(obj)?

To prevent IE from throwing an exception when it encounters console.log, add the following to a site-wide js file:

if (!window.console) console = { log: function () { } };


This makes cross browser testing much easier while in development.

Sunday, April 29, 2012

Contained Javascript Library template

This Javascript template enables a library to be written in its own namespace and handle being included multiple times on a page.
It's based on Douglas Crockford's json2.js library.


var MyLibrary;
if (!MyLibrary) {
    MyLibrary = {};
}


(function () {
    //'use strict';
    if (typeof MyLibrary.myFunction !== 'function') {
        MyLibrary.myFunction = function (arg1, arg2) {
          //...method body here...
          return true;
        };
    }
}());

Tuesday, July 12, 2011

python SimpleHTTPServer solves XMLHttpRequest errors

Loading an html file in a browser from the filesystem (i.e. file:///) that uses XMLHttpRequest will throw a javascript error since for security purposes file:/// requests have a null origin.

Example error:
XMLHttpRequest cannot load file:///somepath. Origin null is not allowed by Access-Control-Allow-Origin.

The solution is to serve the html file using http from a local webserver.  A very easy and simple webserver to run is offered with python by typing this in the folder you want to serve files from:
python -m SimpleHTTPServer 1234

Then navigate to http://127.0.0.1:1234/somepath to view the file.