Skip to content

Instantly share code, notes, and snippets.

@Janaka-Steph
Created December 29, 2014 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Janaka-Steph/cbf62844df7a430226ad to your computer and use it in GitHub Desktop.
Save Janaka-Steph/cbf62844df7a430226ad to your computer and use it in GitHub Desktop.
Ajax
/* AJAX */
// Fix for Internet explorer
if (!window.location.origin) {
window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
}
// In order that it works both on local and production site
var locationOrigin;
if (document.domain == "localhost") {
locationOrigin = window.location.origin + "/mydomain.fr/";
} else {
locationOrigin = window.location.origin + "/";
}
var ajaxrequest = function(idTrigger, idViewContainer, urlController, sendMethod) {
$(document).ready(function() {
$(idTrigger).click(function(e) {
//if (event.defaultPrevented) return;
e.preventDefault();
e.stopPropagation();
$.ajax({
url: locationOrigin + urlController,
type: sendMethod,
success: function(data, statut, jqXHR) {
//console.log(locationOrigin);
//console.log("window.location :");console.log(window.location);
//console.log("window.location.origin :");console.log(window.location.origin);
//console.log("history :");console.log(history);
$("#content").remove();
breadcrumbs(urlController);
$(data).hide().insertAfter("#breadcrumbs").fadeIn(500);
$(idViewContainer).wrap("<div id='content' class='row small-10'></div>");
$('html, body').animate({scrollTop: 0}, 0);
},
error: function(jqXHR, statut, erreur) {
alert("Sorry bro, an error occurs");
},
statusCode: {
404: function() {
alert("page not found");
},
400: function() {
alert('bad request');
}
}
});
history.pushState({
idTrigger: idTrigger,
idViewContainer: idViewContainer,
urlController: locationOrigin + urlController,
sendMethod: sendMethod},
'titre', locationOrigin + urlController);
});
});
};
window.onpopstate = function(event) {
//console.log(event.state.idTrigger);
//console.log(event.state.idViewContainer);
//console.log("event.state.urlController :");
//console.log(event.state.urlController);
//console.log(event.state.sendMethod);
$.ajax({
url: event.state.urlController,
type: event.state.sendMethod,
success: function(data, statut, jqXHR) {
/*if(event.state === null){
$("#content").remove();
$(data).hide().insertAfter(".breadcrumbs").fadeIn(500);
$("#home-container").wrap("<div id='content' class='row small-9'></div>");
}else{*/
$("#content").remove();
$(data).hide().insertAfter(".breadcrumbs").fadeIn(500);
$(event.state.idViewContainer).wrap("<div id='content' class='row small-10'></div>"); /*}*/
},
error: function(jqXHR, statut, erreur) {
alert("Sorry bro, an error occurs");
},
statusCode: {
404: function() {
alert("page not found");
},
400: function() {
alert('bad request');
}
}
});
/*
* Ajax Menu
* function(idTrigger, idViewContainer, urlController, sendMethod)
*/
// Call Home
ajaxrequest(".ajaxAcceuil", "#home-container", "home", "POST");
// Call Blog
ajaxrequest(".ajaxBlog", "#liste-article", "blog", "POST");
ajaxrequest(".ajaxBlogJavascript", "#liste-article", "blog/javascript", "POST");
ajaxrequest(".ajaxBlogCrypto", "#liste-article", "blog/crypto", "POST");
ajaxrequest(".ajaxBlogTools", "#liste-article", "blog/outils", "POST");
// Call Réalisations
ajaxrequest(".ajaxRealisations", "#realisations-container", "realisations", "POST");
// Call CV
ajaxrequest(".ajaxCurriculum", "#cv-container", "curriculum", "POST");
// Call Contact
ajaxrequest(".ajaxContact", "#contact-container", "contact", "GET");
// // Call Admin
ajaxrequest(".ajaxAdmin", "#admin-container", "auth/index", "GET");
// Call Article - Suite button
$(document).on('click', '.suitelink', function(e) {
//$(".suitelink").click(function(e) {
e.preventDefault();
var href_article = $(this).attr('href');
//console.log(href_article);
$.ajax({
url: locationOrigin + href_article,
type: "POST",
success: function(data, statut, jqXHR) {
$("#content").remove();
$(data).hide().insertAfter("#breadcrumbs").fadeIn(500);
$("#article-container").wrap("<div id='content' class='row small-10'></div>");
$('html, body').animate({scrollTop: 0}, 0);
},
error: function(jqXHR, statut, erreur) {
alert("Sorry bro, an error occurs");
},
statusCode: {
404: function() {
alert("page not found");
},
400: function() {
alert('bad request');
}
}
});
history.pushState({idTrigger: ".suitelink", idViewContainer: "article-container", urlController: href_article, sendMethod: "POST"}, 'titre', locationOrigin + href_article);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment