Added some typeof checks to handle JS errors introduced in VS2014