ubermajestix (owner)

Revisions

  • 164498 ubermaj... Mon Jan 26 11:14:48 -0800 2009
  • 75f923 ubermaj... Mon Jan 26 10:57:33 -0800 2009
  • 62b51f ubermaj... Mon Jan 26 10:57:05 -0800 2009
  • 9d2c2a ubermaj... Mon Jan 26 10:56:06 -0800 2009
  • a27a7e ubermaj... Mon Jan 26 10:55:14 -0800 2009
  • 28899b ubermaj... Mon Jan 26 10:53:53 -0800 2009
gist: 52914 Download_button fork
public
Description:
keep users from navigating away from a very important page
Public Clone URL: git://gist.github.com/52914.git
Embed All Files: show embed
dont_nav_elsewhere.html.erb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# in your layout
<head>
  <%= stylesheet_link_tag'styles' %>
  <%= javascript_include_tag :defaults %>
  <%= yield :script %>
</head>
<body>
 # ...
 
# in the view you don't want your user to navigate away from
# this is nice on very important forms or a multistep process/wizard kind of deal.
 
<% content_for :script do %>
  <script type="text/javascript">
    // set a global js var to control whether or not users see the "don't navigate away please" dialog
    // set this var to false on links or forms that can navigate away from page in an onclick property
    var confirmation_needed = true;
 
    window.onbeforeunload = confirmExit;
    
    function confirmExit(){
      if(confirmation_needed){
       // if you return a string it will be displayed in between "don't navigate away"
       // and "click Ok to navigate away or cancel to stay put" in the confirm dialog
        return "Ahoy!\n\n You done doing this awesomely important task?";
      }
    }
  </script>
<% end %>
 
# below is an example of a link that can navigate away from the page without the above confirm dialog
# note that we set confirmation_needed to false before we do a different confirm message.
 
<%= link_to "cancel what i'm doing", destroy_this_info_path, :onclick => "confirmation_needed = false; confirm('You are about to destroy all this info');"%>
 
# the "don't nav elsewhere" dialog will appear on all forms that try to submit also
# we have to again set confirmation_needed to false, I do this in the submit_tag helper:
 
<% form_tag create_somestuff_path do%>
  <%= text_field_tag :somestuff, "somestuff" %>
  <%= submit_tag "save", :onclick=>"confirmation_needed = false;"%>
<% end%>