คำตอบที่ได้รับเลือกจากเจ้าของกระทู้
ความคิดเห็นที่ 2
ต้องอาศัย history.pushState และ history.popstate
โดยถ้าผู้ใช้เข้ามาที่หน้าเว็บที่ต้องการก็ทำการ pushstate ไว้ เช่น
if ( history.pushState ) history.pushState( {}, document.title, window.location.href);
จากนั้น เราก็ผูกโปรแกรมไว้กับ popstate event เช่น
jQuery(document).ready(function($) {
if (window.history && window.history.pushState) {
//เมื่อผู้ใช้กดปุ่ม back บน browser
$(window).on('popstate', function() {
alert('Back button was pressed.');
//เราสามารถใช้ window.location.href เพื่อรับค่า url ปัจจุบันไปทำอะไรอย่างอื่นต่อไป
//หรือจะบังคับเปลี่ยนหน้าเว็บ เช่น window.location.href = 'http://www.google.com';
});
}
});
เราจะต้อง pushstate เมื่อเข้าหน้าเว็บที่ต้องการก่อนทุกครั้ง ถึงจะมีผลให้เกิด popstate event ที่เกิดจากผู้ใช้ออกจากหน้าดังกล่าวที่เรา pushstate ไว้
โดยถ้าผู้ใช้เข้ามาที่หน้าเว็บที่ต้องการก็ทำการ pushstate ไว้ เช่น
if ( history.pushState ) history.pushState( {}, document.title, window.location.href);
จากนั้น เราก็ผูกโปรแกรมไว้กับ popstate event เช่น
jQuery(document).ready(function($) {
if (window.history && window.history.pushState) {
//เมื่อผู้ใช้กดปุ่ม back บน browser
$(window).on('popstate', function() {
alert('Back button was pressed.');
//เราสามารถใช้ window.location.href เพื่อรับค่า url ปัจจุบันไปทำอะไรอย่างอื่นต่อไป
//หรือจะบังคับเปลี่ยนหน้าเว็บ เช่น window.location.href = 'http://www.google.com';
});
}
});
เราจะต้อง pushstate เมื่อเข้าหน้าเว็บที่ต้องการก่อนทุกครั้ง ถึงจะมีผลให้เกิด popstate event ที่เกิดจากผู้ใช้ออกจากหน้าดังกล่าวที่เรา pushstate ไว้
แสดงความคิดเห็น
เราจะมีทางตรวจสอบ ค่า url จากปุ่ม back บน browser ได้ไหม
php หรือ javascript ก็ได้ ขอบคุณครับ