Js不能用ruby on rails正确加载

js doesn't load properly with ruby on rails

本文关键字:加载 rails on 不能 ruby Js      更新时间:2023-09-26

在我的索引我有<a href="/dashboard/gov.html">Government dashboard</a>链接到我的政府页面,并在我的政府页面我有谷歌地图api和我自己的javascript。但只有一个我自己的js工作正常,当我点击链接到政府页面。奇怪的是,如果我只是重新加载gov.html(或将其设置为root),而不是点击链接重定向,似乎工作很好。

下面是不工作的html部分:

<div  id='map'  style='height:280px; width:400px' />  
<script  src="https://maps.googleapis.com/maps/api/js?key=XXX"></script>  
<script>  
      var  map  =  new  google.maps.Map(document.getElementById('map'),  {  
                  center:  new  google.maps.LatLng(31.267401,  121.522179),  
                  mapTypeId:  google.maps.MapTypeId.ROADMAP,  
                  zoom:  11  
              });  
                              var  t  =  new  Date().getTime();  
      var  waqiMapOverlay  =  new  google.maps.ImageMapType({  
                  getTileUrl:  function(coord,  zoom)  {  
                            return  'http://tiles.aqicn.org/tiles/usepa-aqi/'  +  zoom  +  "/"  +  coord.x  +  "/"  +  coord.y  +  ".png?token=_TOKEN_ID_";  
                  },  
                  name:  "Air  Quality",  
        });  
      map.overlayMapTypes.insertAt(0,waqiMapOverlay);  
</script>
    </th>
    <th><img id="imgtraffic" width="395" height="280" /></th>
  </tr>
</table>
<table align="center" width="811" border="1">
  <tr>
    <td bgcolor="#9AD7F1" width="811">City Heat Map</td>
  </tr>
  <tr>
    <td><div id="container"></div>
     <div class="button-group">
      <input type="button" class="button" value="Show heat map" onclick="heatmap.show()"/>
      <input type="button" class="button" value="Close heat map" onclick="heatmap.hide()"/>
    </div>
    <script>
        var map = new AMap.Map("container", {
        resizeEnable: true,
        center: [116.418261, 39.921984],
        zoom: 11
    });
    if (!isSupportCanvas()) {
         ...
    </script>
    </td>
  </tr>
</table>

非常感谢你的帮助

试着把你的代码放到

$( document ).on('turbolinks:load', function() {
  Your code here
})

谢谢大家,实际上只需修改链接的属性

<%= link_to 'Government dashboard', "/dashboard/gov.html", data: { turbolinks: 'false'} %>