jQuery code still being executed even after being

2019-06-11 01:00发布

I have a basic .js file with this inside it

//$('#show').html('<%= escape_javascript(render(:partial => 'show')) %>');

When the .js file is called, the code above gets executed and the partial is rendered even though it's commented out. When the code is deleted, the partial never gets rendered. The DOM remains unchanged, but I can see the partial being rendered by the server at the command line. What gives?

4条回答
再贱就再见
2楼-- · 2019-06-11 01:36

I had a similar issue and figured out a work around for what I wanted it for. Perhaps something like this will help you out!

Instead of commenting the lines of code out, try just building a logical operator that gives you an "on/off" toggle on the functions you want to run. I set mine up like this so I could turn off the script but keep it for future reference:

var toggle = 1; 

if(toggle == 2){
   functions that you want to run when turned on... 
}

Since your variable is not equal to two, the if statement will not render out what's stored inside of it, effectively 'commenting' that code out for you! To turn it back on all you have to do is change the 'toggle' variable back to 2 (or whatever 'on' value you want). Just a little work around I found for myself, hope that helps!

查看更多
可以哭但决不认输i
3楼-- · 2019-06-11 01:47

You're commenting out the JavaScript--which executes on the client side.

The partial rendering happens on the server side, before the client ever sees the rendered JavaScript.

In other words, commenting out JavaScript has zero effect on the server-side processing. If you don't want the server-side string to be displayed, comment it out:

<%#= escape_javascript(etc) %>

Let's assume the partial renders like this:

<h1>Foo bar baz</h1>
<div>Plugh!</div>

Escaping this for JavaScript will turn the newline into a \n (and would escape single- and double-quotes, etc.) leaving you with this on the client side:

$('#show').html('<h1>Foo bar baz</h1>\n<div>Plugh!</div>');

Whether or not the JS is commented out or not, the partial is going to render unless you comment out the results of the escape_javascript Ruby code.

On the client side, if the JS is commented out, it shouldn't update show's HTML--are you saying it is?

查看更多
甜甜的少女心
4楼-- · 2019-06-11 01:51

Check to see if this is the line or you have this call some place else:

//$('#show').html('<%=escape_javascript(render(:partial => 'show'))%>');alert('This is the line');
查看更多
该账号已被封号
5楼-- · 2019-06-11 01:57

Perhaps your escape_javascript function is inserting a newline? That would end the comment and cause anything after the newline to be executed.

查看更多
登录 后发表回答