Index: django/template/defaultfilters.py
===================================================================
--- django/template/defaultfilters.py	(revision 6525)
+++ django/template/defaultfilters.py	(working copy)
@@ -36,7 +36,7 @@
 
 
 def addslashes(value):
-    "Adds slashes - useful for passing strings to JavaScript, for example."
+    "Backslash-escapes backslashes and quotes - useful for passing strings to CSV or JavaScript, for example."
     return value.replace('\\', '\\\\').replace('"', '\\"').replace("'", "\\'")
 addslashes = stringfilter(addslashes)
 
@@ -45,6 +45,25 @@
     return value and value[0].upper() + value[1:]
 capfirst = stringfilter(capfirst)
 
+def escapejs(value):
+    "Backslash-escapes characters disallowed from JavaScript strings."
+    maps = (
+        ('\\', '\\\\'),
+        ('"', '\\"'),
+        ("'", "\\'"),
+        ('\n', '\\n'),
+        ('\r', '\\r'),
+        ('\b', '\\b'),
+        ('\f', '\\f'),
+        ('\t', '\\t'),
+        ('\v', '\\v'),
+        ('</', '<\\/'),
+        )
+    for bad, good in maps:
+        value = value.replace(bad, good)
+    return value
+escapejs = stringfilter(escapejs)
+
 def fix_ampersands(value):
     "Replaces ampersands with ``&amp;`` entities"
     from django.utils.html import fix_ampersands
Index: tests/regressiontests/defaultfilters/tests.py
===================================================================
--- tests/regressiontests/defaultfilters/tests.py	(revision 6525)
+++ tests/regressiontests/defaultfilters/tests.py	(working copy)
@@ -43,6 +43,18 @@
 >>> capfirst(u'hello world')
 u'Hello world'
 
+>>> escapejs(u'"double quotes" and \'single quotes\'')
+u'\\"double quotes\\" and \\\'single quotes\\\''
+
+>>> escapejs(ur'\ : backslashes, too')
+u'\\\\ : backslashes, too'
+
+>>> escapejs(u'and lots of whitespace: \r\n\t\v\f\b')
+u'and lots of whitespace: \\r\\n\\t\\v\\f\\b'
+
+>>> escapejs(ur'<script>and this</script>')
+u'<script>and this<\\/script>'
+
 >>> fix_ampersands(u'Jack & Jill & Jeroboam')
 u'Jack &amp; Jill &amp; Jeroboam'
 
Index: docs/templates.txt
===================================================================
--- docs/templates.txt	(revision 6525)
+++ docs/templates.txt	(working copy)
@@ -996,9 +996,8 @@
 addslashes
 ~~~~~~~~~~
 
-Adds slashes. Useful for passing strings to JavaScript, for example.
+Backslash-escapes backslashes and quotes - useful for passing strings to CSV or JavaScript, for example.  `escapejs` is preferable for passing strings to JavaScript.
 
-
 capfirst
 ~~~~~~~~
 
@@ -1057,6 +1056,22 @@
     * ``'"'`` (double quote) to ``'&quot;'``
     * ``"'"`` (single quote) to ``'&#39;'``
 
+escapejs
+~~~~~~~~
+
+Backslash-escapes characters disallowed from JavaScript strings.  It makes these changes:
+
+	* ``\`` to ``\\``
+	* ``"`` to ``\"``
+	* ``'`` to ``\'``
+	* *line feed* to ``\n``
+	* *carriage return* to ``\r``
+	* *backspace* to ``\b``
+	* *form feed* to ``\f``
+	* *tab* to ``\t``
+	* *vertical tab* to ``\v``
+	* ``</`` to ``<\/``
+
 filesizeformat
 ~~~~~~~~~~~~~~
 
