Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Form element reloader
- $(document).on("submit", ".js_container-reloader", function(e){
- e.preventDefault();
- //Call the reloader function
- elementReloader(e.currentTarget, "POST");
- });
- //Link element reloader
- $(document).on("click", ".js_container-reloader", function(e){
- e.preventDefault();
- //Call the reloader function
- elementReloader(e.currentTarget, "GET");
- });
- /* Element reloader function
- * Takes an element and the type of request this is */
- function elementReloader(element, type){
- //Set variables
- var element = $(element),
- eleAction = element.attr('action'),
- eleHref = element.attr('href'),
- reloadContainer,
- ajUrl,
- ajType,
- data;
- //Wrap the element in a reloader div and save the div to a variable
- element.wrap("<div class='dv_dis-i js_reload-container'></div>");
- reloadContainer = element.closest('.js_reload-container');
- //If the request should be done by POST
- if(eleAction){
- ajType = "POST";
- ajUrl = eleAction;
- data = element.serialize();
- //If the request should be done by GET
- }else{
- ajType = "GET";
- ajUrl = eleHref;
- }
- //Run the ajax script
- $.ajax({
- type: ajType,
- url: ajUrl,
- data: data,
- success: function(result){
- //Add the result to the wrapper
- reloadContainer.html(result);
- //Remvoe the wrapper
- reloadContainer.find(".js_container-reloader").unwrap();
- },
- error: function(xhr) {
- /* Reload the alerts container
- * An alert is saved to the session if there was an error
- * with any script that was loaded.
- * This function reloads the container which shows the session alert. */
- reloadAlertContainer();
- },
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement