2
我有非常奇怪的要求。檢測用戶是否點擊了「返回」按鈕 - 手機瀏覽器
要求說:在商品詳細頁,當產品形象,在燈箱打開然後按壓在手機,產品詳細頁面瀏覽器back
按鈕將顯示,沒有前一頁即產品網格頁面
所以,我想過如何在跨瀏覽器中處理返回按鈕。一些解決方案在桌面瀏覽器中工作,但沒有在Mobile Browsers
工作,我嘗試以下解決方案:
window.onload = function() {
if (typeof history.pushState === "function") {
history.pushState("jibberish", null, null);
window.onpopstate = function() {
history.pushState('newjibberish', null, null);
// Handle the back (or forward) buttons here
// Will NOT handle refresh, use onbeforeunload for this.
};
}
else {
var ignoreHashChange = true;
window.onhashchange = function() {
if (!ignoreHashChange) {
ignoreHashChange = true;
window.location.hash = Math.random();
// Detect and redirect change here
// Works in older FF and IE9
// * it does mess with your hash symbol (anchor?) pound sign
// delimiter on the end of the URL
}
else {
ignoreHashChange = false;
}
};
}
};
也試過這樣:
if (window.history && window.history.pushState) {
$(window).on('popstate', function() {
var hashLocation = location.hash;
var hashSplit = hashLocation.split("#!/");
var hashName = hashSplit[1];
if (hashName !== '') {
var hash = window.location.hash;
if (hash === '') {
alert('Back button was pressed.');
}
}
});
試過這個問題,以及
window.onbeforeunload = function (e) {
var e = e || window.event;
var msg = ""
$("#blueimp-gallery").hide();
// For IE and Firefox
if (e) {
e.returnValue = msg;
}
// For Safari/chrome
return msg;
};
您的瀏覽器需求是什麼? history.pushState適用於除Opera Mini以外的大多數移動瀏覽器。 – SchattenJager
所有手機瀏覽器。我真的不關心Opera。如果您認爲它在所有瀏覽器中都可用,那麼我應該如何通過按回按鈕來隱藏燈箱,然後在關閉燈箱後按鈕應該表現爲默認行爲 – Gags