如何从Rails视图调用JavaScript函数?

时间:2015-04-20 09:44:37

标签: javascript html ruby-on-rails ruby ruby-on-rails-3

我有弹出新屏幕的JavaScript函数,我需要在Rails视图中使用它们。

这是/app/assets/signup.js中的脚本:

<script type="text/javascript">

  function popup() {
    cuteLittleWindow = window.open("signup.html", "littleWindow", "location=no,width=320,height=200");
  }

</script>

如何在/views/index.html.erb中使用JavaScript函数?

2 个答案:

答案 0 :(得分:0)

将以下代码放在关闭正文标记之前,如下所示:

<body>
  <!-- Your body things -->

  <script>

  $(function(){

    function popup(){ 
      cuteLittleWindow = window.open("signup.html", "littleWindow", "location=no,width=320,height=200"); 
    }

  });

  </script>

</body>

答案 1 :(得分:0)

index.html.erb

中的

#####your view code

<script type="text/javascript">
$(document).ready(function(){

  function popup(){ 
    cuteLittleWindow = window.open("signup.html", "littleWindow", "location=no,width=320,height=200"); 
  }

})//document ends
</script>

使用Rails turbolinks,您必须收听turbolinks:load事件:

$(document).on "turbolinks:load", ->
  alert "page has loaded!"