From 9fc659be672b79b20a0d1fbebc24cf2f4c32eec6 Mon Sep 17 00:00:00 2001 From: Andrew Lorente Date: Wed, 23 Jul 2014 14:21:24 -0700 Subject: [PATCH] Declare function-local variables properly Variables inside if-clauses are indeed hoisted to the top of the enclosing scope. It's bad form to make `var` declarations inside an if-clause. sel is now function-local, rather than global :( --- static/application.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/static/application.js b/static/application.js index 2ba304f..44946a7 100644 --- a/static/application.js +++ b/static/application.js @@ -362,12 +362,13 @@ haste.prototype.configureShortcuts = function() { ///// Tab behavior in the textarea - 2 spaces per tab $(function() { $('textarea').keydown(function(evt) { + var sel, startPos, endPos, scrollTop, + myValue = ' '; if (evt.ctrlKey || evt.keyCode !== 9) { return true; } evt.preventDefault(); - var myValue = ' '; // http://stackoverflow.com/questions/946534/insert-text-into-textarea-with-jquery // For browsers like Internet Explorer if (document.selection) { @@ -378,9 +379,9 @@ $(function() { } // Mozilla and Webkit else if (this.selectionStart || this.selectionStart == '0') { - var startPos = this.selectionStart; - var endPos = this.selectionEnd; - var scrollTop = this.scrollTop; + startPos = this.selectionStart; + endPos = this.selectionEnd; + scrollTop = this.scrollTop; this.value = this.value.substring(0, startPos) + myValue + this.value.substring(endPos,this.value.length); this.focus();