Acquire Asset Path from JavaScript

前端 未结 6 1012
梦如初夏
梦如初夏 2021-02-18 16:17

I need to display images on an HTML5 canvas that are in the Rails asset pipeline, but I need to know the path for the asset from JavaScript. I\'m using js-routes for other parts

6条回答
  •  青春惊慌失措
    2021-02-18 16:30

    In my case, I wanted to get the stylesheet path and the hash that rails generates for cache busting made it impossible to hardcode.

    What ended up working quite well for me is to assign an ID to the main stylesheet link element in the html (layout) and then use javascript to extract the href. If you want the base asset path, perhaps create a generic element with the data you need as an attribute.

    Rendered HTML

    
    

    JS

    $("#main-css").attr("href"); // "mypath/main.css"
    

提交回复
热议问题