0

Can I have conditional javascript in my show.erb.html files?

Something like this:

<%- if File.exists?("/leggle/data/tree_xml.xml") %>
  <script type="text/javascript"> 
    ...code...
  </script>
<%- end %>

and have the javascript code appear only if that file exists??

2 Answers 2

2

Yes you can. The better way to do it though is to use unobtrusive JavaScript (UJS) which will keep JS and HTML separated (just like Model/View/Controller). In your case you'll either have or not have a tag (for example) with a JavaScript with assigned action to it.

Sign up to request clarification or add additional context in comments.

Comments

1

Yes. With something like this, it's always easy to just try it out, visit the page, and check the page source to see if it's there (or see if the JS action happened).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.