简体   繁体   中英

Call a js file after ajax call in Rails

I am using prismjs for syntax highlighting in my Rails app. I have included the stylesheet and js file in my layout. My problem is that code is highlighted only once on page load. Whenever the partial refreshes after an ajax call, the code highlight does not work. If I include the prismjs file in partial then it works. Is there any better way to make this work?

layout.html.erb

<%= stylesheet_link_tag :prism %>
<%= javascript_include_tag :prism %>

This works on page load for the first time. Then to make it work on every ajax call response I must do this:

partial.html.erb

<%= javascript_include_tag :prism %>

Is there any other better way to achieve the same like call this file from a customevent or any other method?

If you update your HTML either with HTML response from the Ajax call or from JSON updating the HTML. The content of that part of the page gets changed and the effect on PrismJS being loaded on the page load goes away, so you need to call it again for the highlight to work.

Here are the methods you will need for this. Once you update the HTML just call either highlightAll or highlightAllUnder if you want to be more localized in your update.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM