JQuery html () and newline characters

I am using jQuery with rails and have the following code snippet

$('#related').html("<%= render :partial => "related_items" %>");

I have a problem in the browser where the contents of the linked element are replaced only when there are no line breaks in the partial.

This is not too much, I can put everything on one line, but it makes the code very difficult to read.

Is there a way to get around line breaks in the jQuery html () attribute?

+3
source share
3 answers

What happens here is the code between <%and %>first interpreted by Rails, and the output of this interpretation is sent to the browser. So what goes to the browser:

$('#related').html("Some text with a
line break in it");

javascript, javascript . , javascript.

+3

, , , .

$('#related').html("<%= escape_javascript(render :partial => "related_items") %>");
+2

... Wait, should this code not be processed before running javascript? Or are you trying to print the code in text format for educational reasons?

0
source

Source: https://habr.com/ru/post/1711701/


All Articles