diff --git a/lib/codemirror.js b/lib/codemirror.js
index <HASH>..<HASH> 100644
--- a/lib/codemirror.js
+++ b/lib/codemirror.js
@@ -266,6 +266,7 @@ var CodeMirror = (function() {
     connect(code, "mousedown", operation(onMouseDown));
     connect(code, "dblclick", operation(onDblClick));
     connect(code, "scroll", updateDisplay);
+    connect(window, "resize", updateDisplay);
     connect(input, "keyup", operation(onKeyUp));
     connect(input, "keydown", operation(onKeyDown));
     connect(input, "focus", onFocus);
@@ -575,6 +576,7 @@ var CodeMirror = (function() {
     }
 
     function updateDisplay(changes) {
+      if (!code.clientWidth) return;
       var lh = lineHeight(), top = code.scrollTop - space.offsetTop;
       var visibleFrom = Math.max(0, Math.floor(top / lh));
       var visibleTo = Math.min(lines.length, Math.ceil((top + div.clientHeight) / lh));
@@ -1137,7 +1139,8 @@ var CodeMirror = (function() {
       }),
       replaceRange: operation(replaceRange),
 
-      operation: function(f){return operation(f)();}
+      operation: function(f){return operation(f)();},
+      refresh: function(){updateDisplay([{from: 0, to: lines.length}]);}
     };
     return instance;
   }
add .refresh() method for calling after resizing the editor