0

In my rails project, I want to trigger an event when we close a specific window, let's say I have home view.

I can acheive that by adding something like:

<script type="text/javascript">
    $(window).unload(function() {
      alert('hey');
    });
</script>

to the end of /home/index.html.erb file.

If I move the script to home.js.coffee it will stop from working.

I want to trigger the alert when I close home page, not other pages, how can I achieve that?

Eki Eqbal
  • 5,779
  • 9
  • 47
  • 81
  • 3
    Don't do that… this is extremely unreliable. If you really want to do that (and I highly doubt that), give your body element an idea and check if that ID is available. If yes, set your unload handler. All JS files get loaded always in the default configuration. – ckruse Oct 02 '14 at 18:52
  • Thanks for your willing to help @ckruse, can you provide some code of how to achieve that? – Eki Eqbal Oct 02 '14 at 18:58
  • Well, give your body element an ID (for example `foo`) and check for that existance (`if($("#foo").length > 0)`). If it exists set your event handler. – ckruse Oct 02 '14 at 19:17

0 Answers0