开发者

Rails: erb + javascript question

开发者 https://www.devze.com 2023-04-05 02:30 出处:网络
Can I have conditional javascript in my show.erb.html files? Something like this: <%- if File.exists?(\"/leggle/data/tree_xml.开发者_运维百科xml\") %>

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??


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消