I have next js.erb file:
<% if Rails.env == 'production' %>
<% if request.env['SERVER_NAME'] == 'example.com' %>
$(function () {
...
...
});
<% end %>
<% end %>
How to convert second line in JavaScript without Ruby?
Output the SERVER_NAME
as a string, and do the comparison client-side:
<% if Rails.env == 'production' %>
if ('<%= request.env['SERVER_NAME'] %>' == 'example.com') {
$(function () {
...
...
});
}
<% end %>
If the second line can "only contain javascript" (I strongly question this) then you can move the Ruby bit out to a variable:
var serverName = '<%= request.env['SERVER_NAME'] %>';
<% if Rails.env == 'production' %>
if (serverName == 'example.com') {
$(function () {
...
...
});
}
<% end %>
I found the answer as:
<% if Rails.env == 'production' %>
if (window.location.hostname == 'example.com') {
$(function () {
...
...
});
}
<% end %>
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.