Last active
August 29, 2015 14:00
-
-
Save TheRyanHickman/11407215 to your computer and use it in GitHub Desktop.
Ajax Page Loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
checkjQuery = false; | |
jQueryScriptOutputted = false; | |
//Content ID | |
var AJAX_content = 'contenuto'; | |
//Search Class | |
var AJAX_search_class = 'form-search'; | |
var AJAX_ignore_string = new String('#, /wp-, .pdf, .zip, .rar'); | |
var AJAX_ignore = AJAX_ignore_string.split(', '); | |
var AJAX_track_analytics = true; | |
var AJAX_scroll_top = true; | |
var AJAX_warnings = false; | |
var AJAX_reloadDocumentReady = false; | |
//Dont mess with these... | |
var AJAX_isLoad = false; | |
var AJAX_started = false; | |
var AJAX_searchPath = null; | |
var AJAX_ua = jQuery.browser; | |
//urls | |
var AAPLsiteurl = "http://riotdesign.eu"; | |
var AAPLhome = "http://riotdesign.eu"; | |
var AAPLloadingIMG = jQuery('<img/>').attr('src', ''); | |
var AAPLloadingDIV = jQuery('<div/>').attr('style', 'display:none;').attr('id', 'ajaxLoadDivElement'); | |
AAPLloadingDIV.appendTo('body'); | |
AAPLloadingIMG.appendTo('#ajaxLoadDivElement'); | |
//Loading/Error Code | |
var str = "<center class=\"loader\">\r\n\r\n<\/center>"; | |
var AJAX_loading_code = str.replace('{loader}', AAPLloadingIMG.attr('src')); | |
str = "<center class=\"loader\">\r\n\t<p style=\"text-align: center !important;\">Error!<\/p>\r\n\t<p style=\"text-align: center !important;\">\r\n\t\tErrore: la pagina non è stata caricata.\r\n\t<\/p>\r\n<\/center>"; | |
var AJAX_loading_error_code = str.replace('{loader}', AAPLloadingIMG.attr('src')); /* funzione principale ajax reload */ | |
function AJAX_loadPageInit(scope) { | |
jQuery(scope + "a:not(.noajax)").click(function(event) { | |
//if its not an admin url, or doesnt contain # | |
if (this.href.indexOf(AAPLhome) >= 0 && AJAX_check_ignore(this.href) == true && $( "body" ).hasClass( "logged-in" ) === false ) { | |
// stop default behaviour | |
event.preventDefault(); | |
// remove click border | |
this.blur(); | |
// get caption: either title or name attribute | |
var caption = this.title || this.name || ""; | |
// get rel attribute for image groups | |
var group = this.rel || false; | |
//Load click code - pass reference. | |
try { | |
AJAX_click_code(this); | |
} catch (err) { | |
if (AJAX_warnings == true) { | |
txt = "ERROR: \nThere was an error with click_code.\n"; | |
txt += "Error description: " + err.message; | |
alert(txt); | |
} | |
} | |
// display the box for the elements href | |
AJAX_loadPage(this.href); | |
} | |
}); | |
} | |
function AJAX_loadPage(url, push, getData) { | |
if (!AJAX_isLoad) { | |
AJAX_isLoad = true; | |
//enable onpopstate | |
AJAX_started = true; | |
//AJAX Load page and update address bar url! :) | |
//get domain name... | |
nohttp = url.replace("http://", "").replace("https://", ""); | |
firstsla = nohttp.indexOf("/"); | |
pathpos = url.indexOf(nohttp); | |
path = url.substring(pathpos + firstsla); | |
//Only do a history state if clicked on the page. | |
if (push != 1) { | |
//TODO: implement a method for IE | |
if (typeof window.history.pushState == "function") { | |
var stateObj = { | |
foo: 1000 + Math.random() * 1001 | |
}; | |
history.pushState(stateObj, "ajax page loaded...", path); | |
} | |
} | |
//start changing the page content. | |
var contentHeight = jQuery('#' + AJAX_content).height(); | |
jQuery('#' + AJAX_content).css("height", contentHeight+"px"); //applica manualmente l'altezza del contenuto | |
jQuery('#' + AJAX_content).fadeOut(function() { | |
jQuery('#navbar-top').fadeOut(); | |
document.getElementById(AJAX_content).innerHTML = AJAX_loading_code; | |
if (AJAX_scroll_top == true) { | |
jQuery('html,body').scrollTop(0); | |
} | |
jQuery('#' + AJAX_content).fadeIn(function() { | |
jQuery.ajax({ | |
type: "GET", | |
url: url, | |
data: getData, | |
cache: false, | |
dataType: "html", | |
success: function(data) { | |
AJAX_isLoad = false; | |
jQuery('#navbar-top').fadeIn(); | |
//get title attribute | |
datax = data.split('<title>'); | |
titlesx = data.split('</title>'); | |
if (datax.length == 2 || titlesx.length == 2) { | |
datax = data.split('<title>')[1]; | |
titles = data.split('</title>')[0]; | |
//set the title? | |
jQuery(document).attr('title', (jQuery("<div/>").html(titles).text())); | |
} | |
//Google analytics? | |
if (AJAX_track_analytics == true) { | |
if (typeof _gaq != "undefined") { | |
if (typeof getData == "undefined") { | |
getData = ""; | |
} else { | |
getData = "?" + getData; | |
} | |
_gaq.push(['_trackPageview', path + getData]); | |
} else { | |
if (AJAX_warnings == true) { | |
alert("WARNING: \nAnalytics non è stato inizializzato!"); | |
} | |
} | |
} | |
try { | |
AJAX_data_code(data); | |
} catch (err) { | |
if (AJAX_warnings == true) { | |
txt = "ERROR: \n c'è un errore con data_code.\n"; | |
txt += "Error description: " + err.message; | |
alert(txt); | |
} | |
} | |
//get content | |
data = data.split('id="' + AJAX_content + '"')[1]; | |
data = data.substring(data.indexOf('>') + 1); | |
var depth = 1; | |
var output = ''; | |
while (depth > 0) { | |
temp = data.split('</div>')[0]; | |
//count occurrences | |
i = 0; | |
pos = temp.indexOf("<div"); | |
while (pos != -1) { | |
i++; | |
pos = temp.indexOf("<div", pos + 1); | |
} | |
//end count | |
depth = depth + i - 1; | |
output = output + data.split('</div>')[0] + '</div>'; | |
data = data.substring(data.indexOf('</div>') + 6); | |
} | |
//put the resulting html back into the page! | |
document.getElementById(AJAX_content).innerHTML = output; | |
jQuery('#' + AJAX_content).css("height", 'auto'); //reimposta height automatica | |
//move content area so we cant see it. | |
jQuery('#' + AJAX_content).css("position", "absolute"); | |
jQuery('#' + AJAX_content).css("left", "20000px"); | |
//show the content area | |
jQuery('#' + AJAX_content).show(); | |
//recall loader so that new URLS are captured. | |
AJAX_loadPageInit("#" + AJAX_content + " "); | |
if (AJAX_reloadDocumentReady == true) { | |
jQuery(document).trigger("ready"); | |
} | |
try { | |
AJAX_reload_code(); | |
} catch (err) { | |
if (AJAX_warnings == true) { | |
txt = "ERROR: \nC'è un errore con reload_code.\n"; | |
txt += "Error description: " + err.message; | |
alert(txt); | |
} | |
} | |
//now hide it again and put the position back! | |
jQuery('#' + AJAX_content).hide(); | |
jQuery('#' + AJAX_content).css("position", ""); | |
jQuery('#' + AJAX_content).css("left", ""); | |
jQuery('#' + AJAX_content).fadeIn("slow", function() {}); | |
}, | |
error: function(jqXHR, textStatus, errorThrown) { | |
//Would append this, but would not be good if this fired more than once!! | |
AJAX_isLoad = false; | |
document.title = "Error loading requested page!"; | |
if (AJAX_warnings == true) { | |
txt = "ERROR: \nThere was an error with AJAX.\n"; | |
txt += "Error status: " + textStatus + "\n"; | |
txt += "Error: " + errorThrown; | |
alert(txt); | |
} | |
document.getElementById(AJAX_content).innerHTML = AJAX_loading_error_code; | |
} | |
}); | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using History.js