Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent a browser from going back/forward in history when scrolling horizontally?

How can one disable a modern browsers default functionality using JQuery or Native JS for going backwards or forwards when scrolling horizontally?

This usually happens when using a trackpad and when scrolled to the end or start of a scrollable div.

like image 656
Steve Chan Avatar asked Apr 24 '13 03:04

Steve Chan


People also ask

How do I disable Chrome two finger back forward navigation?

Disable Google Chromes Gestures You can disable chromes gestures by going to System Preference > Mouse OR Trackpad > More Gestures > and uncheck Swipe between pages.

How do I stop horizontal scrolling on a responsive website?

If you want to prevent this you have to check your body elements which is larger than the body. It happens when you take a div with some "padding" or "margin" outside ". container" class (twitter bootstrap). So remove all padding and margin outside the container class.

How do I turn off horizontal scrolling in Chrome?

To hide the horizontal scrollbar and prevent horizontal scrolling, use overflow-x: hidden: HTML.

How do I make my Web page scroll horizontally?

To enable horizontal scrolling, we can use the CSS property overflow-x. If we assign the value scroll to the overflow-x property of the container element, the browser will hide horizontally overflowing content and make it accessible via horizontal scrolling.


2 Answers

history.pushState(null, null, location.href);
window.onpopstate = function(event) {
    history.go(1);
};

Demo: http://jsfiddle.net/DerekL/RgDBQ/show/

You won't be able to go back to the previous webpage, unless you spam the back button or hold down the back button and choose the previous entry.

Note: onpopstate (or event onbeforeunload) doesn't seem to work on iOS.

like image 163
Derek 朕會功夫 Avatar answered Sep 19 '22 22:09

Derek 朕會功夫


So I figured since i've created a web app, why not prompt the user for any unsaved changes which will defer the user from loosing any unsaved changes or ending up on the previous or next page in the browser history.

Here is the solution if any one else comes across this problem like I did:

window.onbeforeunload = function(e) {
    return 'Ask user a page leaving question here';
}; 
like image 41
Steve Chan Avatar answered Sep 18 '22 22:09

Steve Chan