Last active
August 29, 2015 14:15
-
-
Save anjianshi/f8902415d5b4ce02b34a to your computer and use it in GitHub Desktop.
在用户点击 <a> 标签时,进行 pushState 操作 (通过 history.js)
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
//灵感来自: https://gist.github.com/tbranyen/1142129 | |
$(document).on("click", "a", function(event) { | |
// 如果一个链接的协议、域名、端口都和当前页面相同,则将其视为应用内的跳转链接,通过 pushState 进行处理; | |
// 否则作为普通外部链接,交由浏览器处理。 | |
// 不过当用户按下 Ctrl 等控制键时,因为涉及到浏览器原生的在新标签/页面打开链接的操作,一切链接都交由浏览器处理。 | |
var keyPressed = event.ctrlKey || event.shiftKey || event.altKey || event.metaKey || event.which === 2; | |
var isSameSite = this.protocol === location.protocol && this.hostname === location.hostname && | |
this.port === location.port; | |
if(!keyPressed && isSameSite) { | |
History.pushState(null, "title", this.href); | |
event.preventDefault(); | |
console.log(event); | |
} | |
}); | |
History.Adapter.bind(window,'statechange',function(){ | |
var state = History.getState(); | |
console.log(state); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment