Skip to content

Instantly share code, notes, and snippets.

@adnan360
Created October 12, 2014 06:28
Show Gist options
  • Save adnan360/afacca42078af84ed98c to your computer and use it in GitHub Desktop.
Save adnan360/afacca42078af84ed98c to your computer and use it in GitHub Desktop.
Makes any wordpress theme ajax-based (jQuery required)
<script type="text/javascript">
jQuery(document).ready(function($) {
/*function bb() {
$( "a" ).unbind();
$( "a" ).bind( "click", function( ev ) {
ev.preventDefault();
$( "#main" ).load( $(this).attr('href') + " #main", bb );
//bb();
return false;
});
}*/
function bb() {
//$( "a" ).unbind();
hostname = new RegExp(location.host);
$('a').each(function(){
var url = $(this).attr("href");
//console.log($(this).attr("href") + " - " + $(this).attr("href").toLowerCase().indexOf("wp"));
if (($(this).attr('href') != "#") && ($(this).attr('href') != "")) {
$(this).unbind();
}
if($(this).attr("href").toLowerCase().indexOf("/wp-") >= 0){
$(this).attr('target', '_blank');
}
else if((hostname.test(url)) || (url.slice(0, 1) == "/")){ //local
$(this).bind( "click", function( ev ) {
ev.preventDefault();
//$( "#main" ).load( $(this).attr('href') + " #main", bb );
$.ajax({
url: $(this).attr('href'),
success: function(html) {
var content = $('<div />').html(html).find('#main');
$('#main').html(content);
}
}).done(function() {
bb();
});
return false;
});
}
else if(url.slice(0, 1) == "#"){ //anchor
//$(this).addClass('anchor');
}
else { //external
$(this).attr('target', '_blank');
}
});
}
bb();
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment