Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 1.62 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. jQuery: sharing function between page code and document.ready code
  2. <script type="text/javascript">
  3. $(document).ready(function() {
  4.  
  5.     $( "#someDialog" ).dialog({
  6.         autoOpen: false,
  7.         model: true,
  8.         buttons: {
  9.             "Do Something": function() {
  10.                 var cleanInput = sanitizeInput(input);
  11.                 // Do something with the clean input
  12.             },
  13.             Cancel: function() {
  14.                 $( this ).dialog( "close" );
  15.             }
  16.         },
  17.         close: function() {
  18.         }
  19.     });
  20.  
  21.     function sanitizeInput(input) {
  22.         // Some magic here
  23.         return input;
  24.     }
  25. });
  26. </script>
  27.        
  28. <a href="#" onclick="doSomething('wendy');">Wendy's stats</a>
  29.        
  30. <script type="text/javascript">
  31.  
  32. function doSomething(input) {
  33.     var cleanInput = sanitizeInput(input);
  34.     // Some code here
  35. }
  36.  
  37. </script>
  38.        
  39. $(document).ready(function() {
  40.  
  41.     $( "#someDialog" ).dialog({
  42.         autoOpen: false,
  43.         model: true,
  44.         buttons: {
  45.             "Do Something": function() {
  46.                 var cleanInput = sanitizeInput(input);
  47.                 // Do something with the clean input
  48.             },
  49.             Cancel: function() {
  50.                 $( this ).dialog( "close" );
  51.             }
  52.         },
  53.         close: function() {
  54.         }
  55.     });
  56. });
  57.  
  58.   /*** Make it global ***/
  59. function sanitizeInput(input) {
  60.     // Some magic here
  61.     return input;
  62. }
  63.        
  64. <a href="#" onclick="doSomething('wendy');">Wendy's stats</a>
  65.        
  66. <a href="#" data-name="wendy or server var">Wendy's stats</a>
  67.        
  68. $("a[data-name]").click(function (e) {
  69.     e.preventDefault();
  70.     doSomething($(this).data("name"));
  71. });