@tag
is a string.
Why does this work:
tag_tracker = "<%= @tag %>";
alert(tag_tracker);
But not this?
tag_tracker = <%= @tag %>; // Why is this not read as a string?
alert(tag_tracker);
Thank you in advance!
If you have JS inside of ERB files, you need to ensure that the javacript code that is generated is correct.
Let's assume that you have a string "div"
stored in @tag
The first option:
tag_tracker = "<%= @tag %>";
alert(tag_tracker);
will generate a correct JS with div
wrapped in quotes.:
tag_tracker = "div";
alert(tag_tracker);
The second one:
tag_tracker = <%= @tag %>;
alert(tag_tracker);
will generate JS without quotes around the div
:
tag_tracker = div;
alert(tag_tracker);
and that is incorrect, because in this case div
is interpreted as a variable, not as string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.