Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. //Form element reloader
  2. $(document).on("submit", ".js_container-reloader", function(e){
  3. e.preventDefault();
  4.  
  5. //Call the reloader function
  6. elementReloader(e.currentTarget, "POST");
  7. });
  8.  
  9.  
  10. //Link element reloader
  11. $(document).on("click", ".js_container-reloader", function(e){
  12. e.preventDefault();
  13.  
  14. //Call the reloader function
  15. elementReloader(e.currentTarget, "GET");
  16. });
  17.  
  18.  
  19. /* Element reloader function
  20. * Takes an element and the type of request this is */
  21. function elementReloader(element, type){
  22.  
  23. //Set variables
  24. var element = $(element),
  25. eleAction = element.attr('action'),
  26. eleHref = element.attr('href'),
  27. reloadContainer,
  28. ajUrl,
  29. ajType,
  30. data;
  31.  
  32.  
  33. //Wrap the element in a reloader div and save the div to a variable
  34. element.wrap("<div class='dv_dis-i js_reload-container'></div>");
  35. reloadContainer = element.closest('.js_reload-container');
  36.  
  37.  
  38. //If the request should be done by POST
  39. if(eleAction){
  40. ajType = "POST";
  41. ajUrl = eleAction;
  42. data = element.serialize();
  43.  
  44.  
  45. //If the request should be done by GET
  46. }else{
  47. ajType = "GET";
  48. ajUrl = eleHref;
  49. }
  50.  
  51.  
  52. //Run the ajax script
  53. $.ajax({
  54. type: ajType,
  55. url: ajUrl,
  56. data: data,
  57. success: function(result){
  58.  
  59. //Add the result to the wrapper
  60. reloadContainer.html(result);
  61.  
  62.  
  63. //Remvoe the wrapper
  64. reloadContainer.find(".js_container-reloader").unwrap();
  65. },
  66. error: function(xhr) {
  67.  
  68. /* Reload the alerts container
  69. * An alert is saved to the session if there was an error
  70. * with any script that was loaded.
  71. * This function reloads the container which shows the session alert. */
  72. reloadAlertContainer();
  73. },
  74. });
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement