# Rails - escape\_javascript without all the \\n\\n\\n\\n\\n

**URL:** <https://www.ruby-forum.com/t/rails-escape-javascript-without-all-the-n-n-n-n-n/200774>\
**Category:** Rails\
**Created:** [December 21, 2010, 9:20pm UTC](https://www.ruby-forum.com/t/rails-escape-javascript-without-all-the-n-n-n-n-n/200774 "2010-12-21T21:20:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [December 21, 2010, 9:20pm UTC](https://www.ruby-forum.com/t/rails-escape-javascript-without-all-the-n-n-n-n-n/200774/1 "2010-12-21T21:20:34Z")

</div>

Hello, I’m using escape\_javascript to return a partial to the browser  
via ajax.

Something like:

```
$("#inject").html("<%=escape_javascript(render :partial =>"feed/

```

index")%\>");

Problem is escape\_javascript ends up outputing all kinds of wasted  
space like

```
\n\n\n\n\n \n

```

Is there anyway in Rails to escape\_javascript more efficiently? No  
need for \n or long gaps of spaces?

Thanks

---

<div class="post-metadata">

**Author:** ![CuriousNewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dec6dc/32.png) [@CuriousNewbie](https://www.ruby-forum.com/u/CuriousNewbie)\
**Post date:** [December 21, 2010, 9:29pm UTC](https://www.ruby-forum.com/t/rails-escape-javascript-without-all-the-n-n-n-n-n/200774/2 "2010-12-21T21:29:59Z")

</div>

On Dec 21, 8:19pm, CuriousNewbie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \n\n\n\n\n \n
> 
> Is there anyway in Rails to escape\_javascript more efficiently? No  
> need for \n or long gaps of spaces?

I think you should be looking at your template rather than  
escape\_javascript - escape\_javascript is just faithfully reproducing  
what has been given to it. You can use -%\> instead of %\> in some  
constructs to avoid blank lines

Fred

---

<div class="post-metadata">

**Author:** ![CuriousNewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dec6dc/32.png) [@CuriousNewbie](https://www.ruby-forum.com/u/CuriousNewbie)\
**Post date:** [December 21, 2010, 9:46pm UTC](https://www.ruby-forum.com/t/rails-escape-javascript-without-all-the-n-n-n-n-n/200774/3 "2010-12-21T21:46:53Z")

</div>

CuriousNewbie wrote in post #969870:

> Hello, I’m using escape\_javascript to return a partial to the browser  
> via ajax.
> 
> Something like:
> 
> ```
> $("#inject").html("<%=escape_javascript(render :partial =>"feed/
> 
> ```
> 
> index")%\>");

Don’t do that. Keep your JavaScript files static. ERb in JavaScript is  
a bad idea.

In this case, put the HTML in a hidden div with a known ID, then have  
the JS read that div.

## Best,

Marnen Laibow-Koser

> **[marnen.org: Homepage of Marnen Laibow-Koser](https://marnen.org/)**

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Sent from my iPhone

---

<div class="post-metadata">

**Author:** ![CuriousNewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dec6dc/32.png) [@CuriousNewbie](https://www.ruby-forum.com/u/CuriousNewbie)\
**Post date:** [December 21, 2010, 9:58pm UTC](https://www.ruby-forum.com/t/rails-escape-javascript-without-all-the-n-n-n-n-n/200774/4 "2010-12-21T21:58:17Z")

</div>

I happen to disagree completely, dynamic javascript is very helpful. I  
have never had the whitespace issue, however, so I would perhaps look to  
editing your partial.

---

<div class="post-metadata">

**Author:** ![CuriousNewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dec6dc/32.png) [@CuriousNewbie](https://www.ruby-forum.com/u/CuriousNewbie)\
**Post date:** [December 21, 2010, 10:03pm UTC](https://www.ruby-forum.com/t/rails-escape-javascript-without-all-the-n-n-n-n-n/200774/5 "2010-12-21T22:03:43Z")

</div>

Please quote when replying.

Garrett L. wrote in post #969887:

> I happen to disagree completely, dynamic javascript is very helpful.

Yes – if your overall design is flawed. I’ve done complex Ajax  
development (not with Rails, though) and never needed dynamic JS. I  
maintain that dynamic JS is a design smell, and that the overall design  
should be corrected if dynamic JS starts to look like the appropriate  
solution.

Under what circumstances do you think dynamic JS is appropriate?

> I  
> have never had the whitespace issue, however, so I would perhaps look to  
> editing your partial.

Indeed. Or even a JS minifier.

## Best,

Marnen Laibow-Koser

> **[marnen.org: Homepage of Marnen Laibow-Koser](https://marnen.org/)**

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![CuriousNewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dec6dc/32.png) [@CuriousNewbie](https://www.ruby-forum.com/u/CuriousNewbie)\
**Post date:** [December 22, 2010, 9:17am UTC](https://www.ruby-forum.com/t/rails-escape-javascript-without-all-the-n-n-n-n-n/200774/6 "2010-12-22T09:17:05Z")

</div>

On Dec 21, 2010, at 10:03 PM, Marnen Laibow-Koser wrote:

> 

What do you exactly mean by dynamic… Can you provide some examples  
in which having dynamic JS can lead to design/development issues?

Would you classify JSON as dynamic? In some way, json is just  
like javascript variables.

Also, what do you mean by hidden div? Do you mean you just jam  
the data in it or use it for html templating?

---

<div class="post-metadata">

**Author:** ![CuriousNewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dec6dc/32.png) [@CuriousNewbie](https://www.ruby-forum.com/u/CuriousNewbie)\
**Post date:** [December 22, 2010, 6:05pm UTC](https://www.ruby-forum.com/t/rails-escape-javascript-without-all-the-n-n-n-n-n/200774/7 "2010-12-22T18:05:10Z")

</div>

Please quote when replying.

Matteo L. wrote in post #969994:

> On Dec 21, 2010, at 10:03 PM, Marnen Laibow-Koser wrote:
> 
> > 
> 
> What do you exactly mean by dynamic…

By “dynamic JavaScript”, I mean JavaScript generated on the fly with RJS  
or ERb rather than an unchanging static .js file.

> Can you provide some examples  
> in which having dynamic JS can lead to design/development issues?

I think the use of dynamic JS complicates things needlessly. It also  
prevents the client from caching the JS file. In general, source code  
(behavior) should be static and data should be dynamic. This is true on  
the server side and I believe it’s also true on the client side.

Dynamic code is also harder to test and debug.

> Would you classify JSON as dynamic? In some way, json is just  
> like javascript variables.

I would classify JSON as data. Since data can be dynamic, I think it is  
entirely appropriate for an Ajax request (or whatever) to return dynamic  
JSON, HTML, XML, CSV, or whatever. Usually this would then be processed  
by a static JavaScript file.

Again, my principle is: dynamic data, static source code/behavior.  
Rails’ dynamic JS crams the data in with the source code, where it  
really doesn’t belong. You wouldn’t do that in Ruby; don’t do it in JS.

> Also, what do you mean by hidden div?

A div with display: none in its style.

> Do you mean you just jam  
> the data in it or use it for html templating?

I don’t understand what you’re asking.

## Best,

Marnen Laibow-Koser

> **[marnen.org: Homepage of Marnen Laibow-Koser](https://marnen.org/)**

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Sent from my iPhone

---

<div class="post-metadata">

**Author:** ![CuriousNewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dec6dc/32.png) [@CuriousNewbie](https://www.ruby-forum.com/u/CuriousNewbie)\
**Post date:** [December 23, 2010, 4:56pm UTC](https://www.ruby-forum.com/t/rails-escape-javascript-without-all-the-n-n-n-n-n/200774/8 "2010-12-23T16:56:22Z")

</div>

Matteo L. wrote in post #970246:

> On Dec 22, 2010, at 6:05 PM, Marnen Laibow-Koser wrote:
> 
> > > 
> > 
> > really doesn’t belong. You wouldn’t do that in Ruby; don’t do it in JS.  
> > Ruby is actually NOT like that… Actually everything we love about  
> > rails  
> > is thanks to dynamic code…

That’s not true at all. Most of it is due to metaprogramming, which is  
a very different thing than dynamically generating source code as text  
– in fact, the point of Ruby’s metaprogramming is that you _don’t_ have  
to dynamically generate source code to have dynamic behavior. Please  
get your facts straight.

(There are a few spots deep in the Rails framework where source code is  
generated as strings, presumably for efficiency reasons. I regard these  
as either errors or performance hacks, and I would never attempt to do  
likewise in my own application code.)

> > > Also, what do you mean by hidden div?
> > 
> > A div with display: none in its style.
> > 
> > > Do you mean you just jam  
> > > the data in it or use it for html templating?
> > 
> > I don’t understand what you’re asking.
> 
> Do you use html/js templating, or the hidden div is filled with  
> data?

That’s not an “or” question, is it? Or am I still not understanding  
what you mean?

## Best,

Marnen Laibow-Koser

> **[marnen.org: Homepage of Marnen Laibow-Koser](https://marnen.org/)**

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Sent from my iPhone

---

<div class="post-metadata">

**Author:** ![CuriousNewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dec6dc/32.png) [@CuriousNewbie](https://www.ruby-forum.com/u/CuriousNewbie)\
**Post date:** [December 23, 2010, 9:52am UTC](https://www.ruby-forum.com/t/rails-escape-javascript-without-all-the-n-n-n-n-n/200774/9 "2010-12-23T09:52:24Z")

</div>

On Dec 22, 2010, at 6:05 PM, Marnen Laibow-Koser wrote:

> > 
> 
> really doesn’t belong. You wouldn’t do that in Ruby; don’t do it in JS.  
> Ruby is actually NOT like that… Actually everything we love about  
> rails  
> is thanks to dynamic code…

> > Also, what do you mean by hidden div?
> 
> A div with display: none in its style.
> 
> > Do you mean you just jam  
> > the data in it or use it for html templating?
> 
> I don’t understand what you’re asking.

Do you use html/js templating, or the hidden div is filled with  
data?
