Created
October 23, 2013 14:51
-
-
Save berlysia/7120247 to your computer and use it in GitHub Desktop.
某プロジェクト用 pushStateしたかっただけ とても雑
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
(function ($) { | |
$.fn.setPushState = function (destinationSelector, referenceSelector, appendClass) { | |
var aC = appendClass || 'ps_active'; | |
var selector = $(this).selector; | |
this.click(function (e) { | |
e.preventDefault(); | |
var nextPageUrl = $(this).attr('href'); | |
if (!$(this).hasClass(aC)) { | |
$('.'+aC).removeClass(aC); | |
$(this).addClass(aC); | |
$(destinationSelector).load(nextPageUrl + ' ' + referenceSelector); | |
window.history.pushState(null,null,nextPageUrl); | |
} | |
}); | |
onpopstate = function(e){ | |
var lp = location.pathname; | |
var filename = lp.substring(lp.lastIndexOf('/')+1,lp.length); | |
$('.'+aC).removeClass(aC); | |
$(selector).parent().find('[href*="'+filename+'"]').addClass(aC); | |
$(destinationSelector).load(lp + ' ' + referenceSelector); | |
}; | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment