Guest User

Untitled

a guest
Jun 24th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. progressive enhancement, less complicated
  2.  
  3. (Explain)
  4.  
  5. (These examples assume a predefined my_callback function)
  6.  
  7. Bad:
  8. - fails if there is no javascript
  9. - links are more complicated to construct, harder to read
  10. - ?
  11.  
  12. <script type="text/javascript">
  13.  
  14. function do_submit( url ) {
  15. $.getJSON( url, my_callback );
  16. };
  17.  
  18. </script>
  19. <a href="#" onclick="do_submit('/action?state=1');return false;">Yes</a>
  20. <a href="#" onclick="do_submit('/action?state=0');return false;">No</a>
  21.  
  22. =================================================
  23.  
  24. Better:
  25. - progressive enhancement, links work without javascript
  26. - it gets complicated:
  27. - server needs to test for X-Requested-With header and return html/redirect/json
  28. - search engines can follow links, but you should never give unauthorized users
  29. access to modify things anyways, so this point is moot
  30. <script type="text/javascript">
  31.  
  32. $(function(){
  33.  
  34. $('a').click(function(){
  35. var url = $(this).attr( 'href' );
  36. $.getJSON( url, my_callback );
  37. });
  38.  
  39. });
  40.  
  41. </script>
  42. <a href="/action?state=1">Yes</a>
  43. <a href="/action?state=0">No</a>
  44.  
  45. =================================================
  46.  
  47. Best? (simpleAjax):
  48. - like "Better" but the little details are handled:
  49. - defaults to $.getJSON, but anything can be used with the "request" option
  50. - links unclickable while waiting for their ajax response
  51. <script type="text/javascript">
  52.  
  53. $(function(){
  54.  
  55. $('a').simpleAjax({ complete: my_callback });
  56.  
  57. });
  58.  
  59. </script>
  60. <a href="/action?state=1">Yes</a>
  61. <a href="/action?state=0">No</a>
  62.  
  63. More:
  64. - if used on the form, the entire form is submitted via ajax
  65. - all form elements disabled while waiting for the ajax response
  66. - if used on any form elements, they are submitted when changed
  67. - form element groups, form submit button disabled while waiting for their ajax response
  68. - text inputs and textareas will auto-submit after a timeout
  69. <script type="text/javascript">
  70.  
  71. $(function(){
  72.  
  73. $('form, form :input:not(:submit)').simpleAjax({ complete: my_callback });
  74.  
  75. });
  76.  
  77. </script>
  78. <form action="/action" method="get">
  79. left: <input type="radio" name="position" value="left">
  80. center: <input type="radio" name="position" value="center">
  81. right: <input type="radio" name="position" value="right">
  82. age: <select name="age">
  83. <option value="under18">Under 18</option>
  84. <option value="exactly18">Exactly 18</option>
  85. <option value="over18">Over 18</option>
  86. </select>
  87. status: <input type="checkbox" name="status" value="enabled">
  88. text: <input class="text" type="text" name="text" value="testing, 1 2 3"> text
  89. big text: <textarea name="biggie">biggie</textarea>
  90. <input class="submit" type="submit" name="submit" value="Submit">
  91. </form>
Add Comment
Please, Sign In to add comment